Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up"auto" return type #727
"auto" return type #727
Comments
|
IIRC this was proposed before and always rejected. I don't know where the
discussion went; if you want the opinions of the authors of type checkers
you may be better off posting this to typing-sig.
|
|
#276 |
|
you can probably use https://github.com/Instagram/MonkeyType to add types, though it's runtime (not static-analysis tool) |


I am unsure if this issue should go here or rather to mypy's tracker. I have looked through issues in both
typingandmypyand couldn't find anything related.Consider the following code:
Upon checking with mypy, an expected
Incompatible types in assignment (expression has type "str", variable has type "int")error is emited at [1], but not at [2]. This makes sense, as type ofyisAny. However, e.g. jedi autocompletion library is smart enough to deduce thatf2() -> A. While in the above degenerate example it would be trivial to just property annotate thef2, the real scenario hasAandf1defined in some library, andf2, which does something more useful than just callingf1is defined in user-code. Specific classAis not intended to be directly instantiated by the user, moreover, it may be buried deeply inside module hierarchy, which may also change between library releases.My suggestion is to introduce
typing.Autowhich, when used in return type annotation, would instruct type checker (mypy or any other) to infer return type from the type of expression in return statement. In case of multiple return statements, the pragmatical approach would probably be to infer the base-most class, possibly issuing an error or a warning if types are completely unrelated.Another possibility is to optionally have mypy treat
typing.Anyreturn types in a manner suggested for the above-describedtyping.Autobut I suspect that this would be a breaking change.P.S.
typing.Autoshould also be usable withintyping.Tuple. I am not sure whether it would be desirable to allow inference of tuple in cases when return type is annotated astyping.Auto.