Skip to content

Enforce Type Hints #1282

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

Closed
theonewolf opened this issue Mar 9, 2016 · 7 comments · Fixed by #1285
Closed

Enforce Type Hints #1282

theonewolf opened this issue Mar 9, 2016 · 7 comments · Fixed by #1285

Comments

@theonewolf
Copy link

I would love an option on the command line to force the use of type hints so that all code is statically typed.

Is this possible or easy to accomplish with mypy?

@gvanrossum
Copy link
Member

gvanrossum commented Mar 9, 2016 via email

@theonewolf
Copy link
Author

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.

Thanks for the response @gvanrossum!

@theonewolf
Copy link
Author

Thanks @ddfisher and @gvanrossum!

@zanellia
Copy link

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).

@JukkaL
Copy link
Collaborator

JukkaL commented 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
Copy link

I see. Thanks for the quick reply @JukkaL!

@rkr87
Copy link

rkr87 commented 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:

async def __category_setup(self, config_item: list[str]) -> bool:

call to the above method I had in my code:

enabled = self.__category_setup(path)

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.

enabled: bool = await self.__category_setup(path)

Having the ability to identify all variables without type annotations via mypy would be the best way around this imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants