Skip to content

Enforce Type Hints #1282

Closed
Closed

Description

@theonewolf

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?

Activity

  1. gvanrossum commented on Mar 9, 2016

    @gvanrossum
    Member
  2. theonewolf commented on Mar 10, 2016

    @theonewolf
    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!

  3. theonewolf commented on Mar 15, 2016

    @theonewolf
    Author
  4. zanellia commented on Jun 14, 2019

    @zanellia

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

  5. JukkaL commented on Jun 14, 2019

    @JukkaL
    Collaborator

    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.

  6. zanellia commented on Jun 14, 2019

    @zanellia

    I see. Thanks for the quick reply @JukkaL!

  7. rkr87 commented on Sep 10, 2022

    @rkr87

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions