-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for conditionally defined overloads (#10712)
### 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: ... ```
- Loading branch information
Showing
3 changed files
with
1,185 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Oops, something went wrong.