python 3: type inferencing with mypy?

est

Given the following snippet in python 3

def foo() -> List[X]: pass

class X:
    def bar(self) -> MYTYPE: pass

for the following expression:

[x.bar() for x in foo()]

Can I utilize mypy package to correctly parse the AST of the expression above and guess the type of the result is List[MYTYPE]?

If can not, what's my best choice? any ideas/packages to recommend?

Alex Hall

Yes, it will infer it. Here's a demo:

from typing import List

class MyType:
    pass

class X:
    def bar(self) -> MyType: pass

def foo() -> List[X]: pass  # note the square brackets

bars = [x.bar() for x in foo()]
reveal_type(bars)

Then when you run mypy script.py in a terminal you will see a message:

Revealed type is 'builtins.list[script.MyType*]'

Documentation about reveal_type here.

By the way there was a small problem with your code, it should be List[X] not List(X).

PyCharm will also recognise the types. bars. will give autocomplete options for list methods, and bars[0]. will give options for MyType.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

F# type inferencing

From Dev

Clojure shortfall on type inferencing

From Dev

Rust type inferencing oddity

From Dev

OCaml Type Inferencing

From Dev

Haskell for Lambda Calculus, Type Inferencing

From Java

Generic Methods and Type Inferencing in Java

From Dev

How to make TypeScript type inferencing work for arrays

From Dev

Python mypy don't type check dataclass

From Dev

What is the type of immutable object in Python (for mypy)

From Dev

Function for restricting the type of attributes in an object (Python, mypy)

From Dev

Why does the type-inferencing logic fails with an iteration over a TreeNodeCollection?

From Dev

How Kotlin inferencing return type of function with expression body

From Dev

Python type checker mypy: subprocess.STARTUPINFO is not defined

From Dev

What is type annotation for "any callable but a class" in Python using Mypy?

From Dev

Python type hints: How to use Literal with strings to conform with mypy?

From Dev

Python mypy unable to infer type from union return types

From Dev

how to resolve python mypy error for type annotation in expression

From Dev

Is there any way to override inherited class attribute type in python with mypy?

From Dev

Python - decorating a instance method with mypy (static type checker)

From Python

Type assertion in MyPy

From Dev

mypy "invalid type" error

From Dev

Mypy type of concatenate tuples

From Dev

Unexpected Optional behavior with Python3 typing and mypy

From Dev

Python / Django - Mypy: expression has type "Type[ModelInstance]", variable has type "ModelInstance"

From Dev

How to fix mypy error, "Expression has type Any [misc]", for python type hinting of int __pow__ int?

From Dev

Python mypy Type Hinting shutil.copyfileobj() has incompatible type "Union[HTTPResponse, BinaryIO]"; expected IO[Any]

From Dev

Python typing, mypy infer return type based on class method return type

From Dev

tensorflow pb file inferencing takes more than 3seconds for an image

From Dev

Python mypy incompatible types

Related Related

  1. 1

    F# type inferencing

  2. 2

    Clojure shortfall on type inferencing

  3. 3

    Rust type inferencing oddity

  4. 4

    OCaml Type Inferencing

  5. 5

    Haskell for Lambda Calculus, Type Inferencing

  6. 6

    Generic Methods and Type Inferencing in Java

  7. 7

    How to make TypeScript type inferencing work for arrays

  8. 8

    Python mypy don't type check dataclass

  9. 9

    What is the type of immutable object in Python (for mypy)

  10. 10

    Function for restricting the type of attributes in an object (Python, mypy)

  11. 11

    Why does the type-inferencing logic fails with an iteration over a TreeNodeCollection?

  12. 12

    How Kotlin inferencing return type of function with expression body

  13. 13

    Python type checker mypy: subprocess.STARTUPINFO is not defined

  14. 14

    What is type annotation for "any callable but a class" in Python using Mypy?

  15. 15

    Python type hints: How to use Literal with strings to conform with mypy?

  16. 16

    Python mypy unable to infer type from union return types

  17. 17

    how to resolve python mypy error for type annotation in expression

  18. 18

    Is there any way to override inherited class attribute type in python with mypy?

  19. 19

    Python - decorating a instance method with mypy (static type checker)

  20. 20

    Type assertion in MyPy

  21. 21

    mypy "invalid type" error

  22. 22

    Mypy type of concatenate tuples

  23. 23

    Unexpected Optional behavior with Python3 typing and mypy

  24. 24

    Python / Django - Mypy: expression has type "Type[ModelInstance]", variable has type "ModelInstance"

  25. 25

    How to fix mypy error, "Expression has type Any [misc]", for python type hinting of int __pow__ int?

  26. 26

    Python mypy Type Hinting shutil.copyfileobj() has incompatible type "Union[HTTPResponse, BinaryIO]"; expected IO[Any]

  27. 27

    Python typing, mypy infer return type based on class method return type

  28. 28

    tensorflow pb file inferencing takes more than 3seconds for an image

  29. 29

    Python mypy incompatible types

HotTag

Archive