Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a possibility to show types as their alias, rather than their underlying resolution, in mypy's output #12502

Closed
Fulguritude opened this issue Mar 31, 2022 · 1 comment

Comments

@Fulguritude
Copy link

First of all, I'm sorry if this already exists and I'm just unaware of it.

Feature

Add some command-line argument to mypy to prevent the expansion/resolution of aliases of types (such as large type Unions; large chains of Literals; or heavily nested aliases) to their underlying type.

Pitch

Often, when running mypy, a type alias is "resolved" to its underlying type, which can sometimes severely impede legibility. This tends to happen a lot with Unions and Literals, as well as general chains of aliases.

Say I have an AlphabetDuos enum containing the 26^2 values "AA", "AB", ..., "ZY", "ZZ", and an accompanying literals list (with an alias) Literals_AlphabetDuos = Literal["AA", ..., "ZZ"] (because I need to serialize/deserialize those cleanly, for example). I've typed my input with Literals_AlphabetDuos, but MyPy will often correct you with a massive value of type 'str' cannot be assigned to type Literal["AA", ..., "ZZ"], expanding the 26^2 values and making the terminal output quickly illegible; when it could much more cleanly answer you with value of type 'str' cannot be assigned to type Literals_AlphabetDuos.

A similar issue happens with large Unions, and nesting of types. It's also easy to see how this automatic resolution gets unwieldy when you have things like Alias3 = Dict[AliasB, AliasD] throughout your code, and AliasB and AliasD are large literals or unions etc. Finding the right comma between parentheses or brackets (in a non-prettified string) is an excruciatingly annoying endeavor (copy-paste from the terminal, then regex, is the best I've found...).

Using such a feature could be as simple as:

python3 -m mypy --keep-aliases my_test_file.py

I hope you'll see the value of this feature.

In any case, thanks for the great work; mypy is very useful and appreciated :)

@AlexWaygood
Copy link
Member

Duplicate of #2968

@AlexWaygood AlexWaygood marked this as a duplicate of #2968 Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants