You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ah, I see that is in the git version, not the latest release.
This is great, I'd like to see also something like --disallow-untyped-signatures which disallows function and method declarations without type annotations.
Is there any way to strictly enforce type hints in the current version? I'd like to enforce static typing also for trivial expression like n = 10 (which should be n : int = 10, obviously). This can be rather useful in some contexts, e.g. when trying to generate code in a statically typed language like C for performance reasons (see https://github.com/zanellia/prometeo, for example).
Reacted by Dwayne Robinson, chrispoptic, tomasyany and Ryan Hamel
There is no way to require variables to be annotated if the type can be inferred by mypy, if I understood correctly that this is what you'd like to do.
Activity
gvanrossum commentedon Mar 9, 2016
theonewolf commentedon Mar 10, 2016
Ah, I see that is in the git version, not the latest release.
This is great, I'd like to see also something like
--disallow-untyped-signatureswhich disallows function and method declarations without type annotations.Thanks for the response @gvanrossum!
theonewolf commentedon Mar 15, 2016
Thanks @ddfisher and @gvanrossum!
zanellia commentedon Jun 14, 2019
Is there any way to strictly enforce type hints in the current version? I'd like to enforce static typing also for trivial expression like
n = 10(which should ben : int = 10, obviously). This can be rather useful in some contexts, e.g. when trying to generate code in a statically typed language like C for performance reasons (see https://github.com/zanellia/prometeo, for example).JukkaL commentedon Jun 14, 2019
There is no way to require variables to be annotated if the type can be inferred by mypy, if I understood correctly that this is what you'd like to do.
zanellia commentedon Jun 14, 2019
I see. Thanks for the quick reply @JukkaL!
rkr87 commentedon Sep 10, 2022
Would it be possible to force required annotations on variables even if mypy can infer a type?
I understand why it isn't currently there but it would be handy for certain situations. EG one I've just encountered;
async method:
call to the above method I had in my code:
The above code is incorrect as the method should be awaited, mypy doesn't raise any errors as it infers the variable type as Coroutine.
However, if I'd typed the enabled variable as bool, mypy would've raised typing error prompting me to add in the await flag.
Having the ability to identify all variables without type annotations via mypy would be the best way around this imo.