-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@overload cannot be used with conditional "if TYPE_CHECKING" #9744
Comments
I'm curious, why would you like an |
While it's just a dummy decorator I'd like to keep typing separate from runtime altogether. |
I think this applies to my present issue as well. I am exploring merging trio-stubs (from trio-typing) into Trio itself and the hints for https://github.com/python-trio/trio-typing/blob/f32f17b0f242daf2d42407f383ca581d64b6c299/trio-stubs/__init__.pyi#L567-L682 |
For your use case you could just put the whole stub in an |
Thanks for the workaround. I'll keep subscribed here in case there's a full fix. |
I opened a PR a while ago that would also fix this issue. Still waiting for a review though. #10712 |
@cdce8p |
@blueyed Great to hear that! Unfortunately I'm still waiting for a review on the PR so no idea when it will get merged. I would be able to remove 300 lines in one file alone just from |
Sorry to bother you again @hauntsaninja. Do you have an idea who might be able to review it? |
### Description This PR allows users to define overloads conditionally, e.g., based on the Python version. At the moment this is only possible if all overloads are contained in the same block which requires duplications. ```py from typing import overload, Any import sys class A: ... class B: ... if sys.version_info >= (3, 9): class C: ... @overload def func(g: int) -> A: ... @overload def func(g: bytes) -> B: ... if sys.version_info >= (3, 9): @overload def func(g: str) -> C: ... def func(g: Any) -> Any: ... ``` Closes #9744 ## Test Plan Unit tests have been added. ## Limitations Only `if` is supported. Support for `elif` and `else` might be added in the future. However, I believe that the single if as shown in the example is also the most common use case. The change itself is fully backwards compatible, i.e. the current workaround (see below) will continue to function as expected. ~~**Update**: Seems like support for `elif` and `else` is required for the tests to pass.~~ **Update**: Added support for `elif` and `else`. ## Current workaround ```py from typing import overload, Any import sys class A: ... class B: ... if sys.version_info >= (3, 9): class C: ... if sys.version_info >= (3, 9): @overload def func(g: int) -> A: ... @overload def func(g: bytes) -> B: ... @overload def func(g: str) -> C: ... def func(g: Any) -> Any: ... else: @overload def func(g: int) -> A: ... @overload def func(g: bytes) -> B: ... def func(g: Any) -> Any: ... ```
Bug Report
The handling of
@overload
decorators happens in (code) blocks only, which makes it not find the implementation when using:I think it would be nice / feasible to merge
if TYPE_CHECKING
blocks maybe, so that the parser would handle it together?mypy 0.800+dev.6e99a2db100d794b1bf900746470527cd03fce16
The text was updated successfully, but these errors were encountered: