Well, mypy never infers return types from the code. So we do recommend the trick with the typevar. It's not particularly often that you need this though, so I'm not keen on adding magic to the type system just to make that less verbose. On Wed, Jun 2, 2021 at 8:31 AM Eric Traut <eric@traut.com> wrote:
If a return type annotation is omitted, static type checkers will typically infer the return type from the implementation, by evaluating the types of all returned expressions and unioning them together.
I need to amend my previous statement though. It appears that mypy doesn't implement this capability currently. I would think that it would be straightforward to implement in mypy.
```python from typing import Any
class Foo: def parse(self, data: bytes): return self
@classmethod def bar(cls, *args: Any, **kwargs: Any): return cls()
class Bar(Foo): pass
x = Bar.bar() reveal_type(x) # Pyright says "Bar", mypy says "Any"
y = Bar().parse(b"") reveal_type(y) # Pyright says "Bar", mypy says "Any" ```
--
Eric Traut Contributor to Pyright & Pylance Microsoft Corp _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>