-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Allow disabling implicit Any for type aliases #2696
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
Comments
One option would be to use deprecate |
+1 to Jukka's idea. We have at least three places where an absence of type is equivalent to
Maybe there others that I don't remember. It would be difficult to remember what is the option to disable each usage. |
Hm, I expect that the "x: Iterable" case is very common. Would be worth doing a little experiment with that: find the code that implements this, make it raise an error instead, and see what it catches in some real-world codebases, starting with mypy. FWIW the pattern |
I'm very much in favor of However, this isn't currently allowed (not sure why):
So when we need to define variables that belong to a generic container type, but without the item type being part of the type of the function call arguments, we have to rely on implicit
Therefore I think either the proposed |
But what you expect to be the meaning of this? Type variables are only used for generic functions, and generic types and aliases. What would be the meaning of an unbound type variable in an annotation? |
I agree, My main point was that currently we often rely on implicit |
It feels like a special case is needed for aliases. When But when |
I'm not sure I understand why we'd want to make that distinction. It seems like a good idea to keep the behavior of aliases and classes as similar as possible. |
It's just a hunch based on experience -- I've fallen into this trap several
times. Somehow `A = List[T]` doesn't scream "generic" like `class
A(List[T]):` does.
|
I think this is now fixed since we have |
The issue is somehow discussed here #2690
Here is a sample code that
mypy
allows and eventually fails in runtimeAs described in #606 discussions and https://www.python.org/dev/peps/pep-0484/#type-aliases this is not a problem with
mypy
, but an overlooked mistake in non-parameterizing generic type aliasProcessor
inoutput
.reveal_type
foroutput
:Revealed type is 'def [T in (builtins.list[builtins.int], builtins.dict)] (inp: T
-1, processor: def (Any))'`Processor
is implicitly treated asProcessor[Any]
in this case.Changing
output
todef output(inp: T, processor: Processor[T]) -> None:
resolves the issue and letsmypy
catch the bug.The problem is that implicit
Any
for type aliases can easily go unnoticed.Suggestions:
As suggested by @roganov & @ilevkivskyi : Add
--disable-implicit-any
flag to mypy, which will disable this behavior and forces the user to be explicit. Drawback, users who do not know about the flag might learn about the bug the hard way (in production 😉 )Disable implicit
Any
by default for type aliases, and force users to be explicit (either by parameterizing[Any]
or[T]
). Drawback, might introduce a lot of mypy errors for existing, "error free" code base (confusing/annoying to users). Also, not entirely sure about how this impacts other scenarios.Produce
warning
when used type alias has no explicit parameterization. This will make mypy exit without failure, but just point the user to potential bugs. Drawback,warnings
might be annoying for users who are ok with their codebase (i.e. might require--suppress-implicit-any-warning
)Any other suggestion ... 😄
Thanks,
The text was updated successfully, but these errors were encountered: