Created
January 25, 2026 11:10
-
-
Save mypy-play/048808c3efced2ebb5eb32944586e020 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Any, Optional, cast, TypeVar, reveal_type | |
| ## Case 1: type variable in return position of function | |
| def f[T]() -> T: | |
| raise Exception() | |
| # mypy correctly refuses to type this, as it cannot infer T from the call site. | |
| reveal_type(f()) | |
| ## Case 2: type variable in return position of method | |
| class C: | |
| def f[T](self) -> T: | |
| raise Exception() | |
| # mypy correctly refuses to type this as well, and for the same reason. | |
| reveal_type(C().f()) | |
| ## Case 3: type variable in return position of method, combined with another | |
| ## type | |
| class D: | |
| def f[T](self) -> Optional[T]: | |
| raise Exception() | |
| # mypy does not return an error or warning in this case, but substitutes | |
| # `Never` for `T`, resulting in `Never | None` being narrowed to `None`. | |
| reveal_type(D().f()) | |
| ## Case 4: two type variables in return position of method, combined in a | |
| ## type form | |
| class E: | |
| def f[T, U](self) -> T | U: | |
| raise Exception() | |
| # mypy also does not return an error or warning in this case, but substiutes | |
| # `Never` for both `T` and `U`, resulting in `Never | Never` being narrowed | |
| # to `Never`. | |
| reveal_type(E().f()) | |
| ## Case 5: type variable with explicit default=Any in return position of | |
| ## function | |
| U = TypeVar("U", default=Any) | |
| def g[U]() -> U: | |
| raise Exception() | |
| # Despite `U` having an explicit default, the definition of `g` returns an | |
| # error, and `g()` is narrowed to `Never` instead of `Any`. | |
| reveal_type(g) | |
| reveal_type(g()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment