Skip to content

Adapt ast types for mypy 0.990  #9129

Closed

Description

@kasium

With mypy 0.990 the complex/int/float promotion was changed (see python/mypy#14030).
Therefore checks like isinstance(node, ast.Num) and isinstance(node.n, int) fails with strict mode enabled.

The recommendation by mypy is to change complex to Union[int, float, complex]. Are there any objections to do so in the ast module?

Activity

  1. srittau commented on Nov 8, 2022

    @srittau
    Collaborator

    I'm not sure what you would want to change. Stubs don't use isinstance checks.

  2. kasium commented on Nov 8, 2022

    @kasium
    ContributorAuthor

    Ah sorry;

    so in my code I use code like isinstance(node, ast.Num) and isinstance(node.n, int) and this fails now with mypy 0.990 (see python/mypy#14030). The recommendation by mypy is to adapt the types of ast.Num.n to be Union[int, float, complex] instead of complex (which implies int and float but not explicitly enough for mypy).

    Proposal: change complex types in _ast.pyi to be Union[int, float, complex]

  3. ilevkivskyi commented on Nov 8, 2022

    @ilevkivskyi
    Member

    FWIW I also think n for both Num and Constant should be Union[int, float, complex]. This is more correct, and doesn't implicitly rely on type promotions.

  4. kasium commented on Nov 8, 2022

    @kasium
    ContributorAuthor

    Okay, let me then give a PR a try

  5. added a commit that references this issue on Nov 8, 2022
    dc60875
  6. ilevkivskyi commented on Nov 8, 2022

    @ilevkivskyi
    Member

    Hm, as noticed in the PR, it looks like tpyeshed is using a flake8 lint rule that actually prohibits int | float. This is bad, there is a chance there are some other places with incorrect plain complex annotations.

    @srittau I would propose to disable this rule globally.

  7. ilevkivskyi commented on Nov 8, 2022

    @ilevkivskyi
    Member

    I briefly scrolled through and found only other problematic annotations in typed-ast https://github.com/python/typeshed/blob/main/stubs/typed-ast/typed_ast/ast27.pyi#L241 and https://github.com/python/typeshed/blob/main/stubs/typed-ast/typed_ast/ast3.pyi#L260. @kasium maybe you can add these to your PR?

  8. kasium commented on Nov 8, 2022

    @kasium
    ContributorAuthor

    sure!

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