-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Support expand_type with ParamSpec.{args,kwargs}
#20119
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
Open
sterliakov
wants to merge
7
commits into
python:master
Choose a base branch
from
sterliakov:bugfix/gh-19839-paramspec-reveal-crash
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−9
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e13a50d
Add logic for ParamSpec args/kwargs in expand_type
sterliakov c08ce63
Remove get_proper_type (though it probably would be OK to increase th…
sterliakov 74e51ec
Refactor into separate methods
sterliakov c01514c
Add test case with Concatenate
sterliakov a11f4e7
Merge branch 'master' into bugfix/gh-19839-paramspec-reveal-crash
sterliakov 2047a96
Restart CI
sterliakov 0158dfe
Merge branch 'master' into bugfix/gh-19839-paramspec-reveal-crash
sterliakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2599,3 +2599,97 @@ def run3(predicate: Callable[Concatenate[int, str, _P], None], *args: _P.args, * | |
| # E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "str" \ | ||
| # E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "_P.args" | ||
| [builtins fixtures/paramspec.pyi] | ||
|
|
||
| [case testRevealBoundParamSpecArgs] | ||
| from typing import Callable, Generic, ParamSpec | ||
| from typing_extensions import TypeVarTuple, Unpack | ||
|
|
||
| P = ParamSpec("P") | ||
| Ts = TypeVarTuple("Ts") | ||
|
|
||
| class Sneaky(Generic[P]): | ||
| def __init__(self, fn: Callable[P, object], *args: P.args, **kwargs: P.kwargs) -> None: | ||
| self.fn = fn | ||
| self.args = args | ||
| self.kwargs = kwargs | ||
|
|
||
| def f1() -> int: | ||
| return 0 | ||
| def f2(x: int) -> int: | ||
| return 0 | ||
| def f3(x: int, /) -> int: | ||
| return 0 | ||
| def f4(*, x: int) -> int: | ||
| return 0 | ||
| def f5(x: int, y: int = 0) -> int: | ||
| return 0 | ||
| def f6(x: int, *args: int) -> int: | ||
| return 0 | ||
| def f7(x: int, *args: Unpack[Ts]) -> int: | ||
| return 0 | ||
| def f8(x: int, *args: Unpack[tuple[str, ...]]) -> int: | ||
| return 0 | ||
| def f9(x: int, *args: Unpack[tuple[str, int]]) -> int: | ||
| return 0 | ||
|
|
||
| reveal_type(Sneaky(f1).args) # N: Revealed type is "tuple[()]" | ||
| reveal_type(Sneaky(f2, 1).args) # N: Revealed type is "Union[tuple[()], tuple[builtins.int]]" | ||
| reveal_type(Sneaky(f3, 1).args) # N: Revealed type is "tuple[builtins.int]" | ||
| reveal_type(Sneaky(f4, x=1).args) # N: Revealed type is "tuple[()]" | ||
| reveal_type(Sneaky(f5, 1).args) # N: Revealed type is "Union[tuple[()], tuple[builtins.int], tuple[builtins.int, builtins.int]]" | ||
| reveal_type(Sneaky(f5, 1, 2).args) # N: Revealed type is "Union[tuple[()], tuple[builtins.int], tuple[builtins.int, builtins.int]]" | ||
| reveal_type(Sneaky(f6, 1).args) # N: Revealed type is "Union[tuple[()], tuple[builtins.int], tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]]" | ||
| reveal_type(Sneaky(f6, 1, 2).args) # N: Revealed type is "Union[tuple[()], tuple[builtins.int], tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]]" | ||
| reveal_type(Sneaky(f7, 1, 2).args) # N: Revealed type is "tuple[Literal[1]?, Literal[2]?]" | ||
| reveal_type(Sneaky(f8, 1, '').args) # N: Revealed type is "Union[tuple[()], tuple[builtins.int], tuple[builtins.int, Unpack[builtins.tuple[builtins.str, ...]]]]" | ||
| reveal_type(Sneaky(f9, 1, '', 0).args) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.int]" | ||
| [builtins fixtures/paramspec.pyi] | ||
|
|
||
| [case testRevealBoundParamSpecKwargs] | ||
| from typing import Callable, Generic, ParamSpec | ||
| from typing_extensions import Unpack, NotRequired, TypedDict | ||
|
|
||
| P = ParamSpec("P") | ||
|
|
||
| class Sneaky(Generic[P]): | ||
| def __init__(self, fn: Callable[P, object], *args: P.args, **kwargs: P.kwargs) -> None: | ||
| self.fn = fn | ||
| self.args = args | ||
| self.kwargs = kwargs | ||
|
|
||
| class Opt(TypedDict): | ||
| y: int | ||
| z: NotRequired[str] | ||
|
|
||
| def f1() -> int: | ||
| return 0 | ||
| def f2(x: int) -> int: | ||
| return 0 | ||
| def f3(x: int, /) -> int: | ||
| return 0 | ||
| def f4(*, x: int) -> int: | ||
| return 0 | ||
| def f5(x: int, y: int = 0) -> int: | ||
| return 0 | ||
| def f6(**kwargs: int) -> int: | ||
| return 0 | ||
| def f7(x: int, **kwargs: str) -> int: | ||
| return 0 | ||
| def f8(x: int, /, **kwargs: str) -> int: | ||
| return 0 | ||
| def f9(x: int, **kwargs: Unpack[Opt]) -> int: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens in examples here and and above if the callable is itself generic (including variadic/paramspec-generic)? |
||
| return 0 | ||
|
|
||
| reveal_type(Sneaky(f1).kwargs) # N: Revealed type is "builtins.dict[builtins.str, Never]" | ||
| reveal_type(Sneaky(f2, 1).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int})" | ||
| reveal_type(Sneaky(f3, 1).kwargs) # N: Revealed type is "builtins.dict[builtins.str, Never]" | ||
| reveal_type(Sneaky(f4, x=1).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x': builtins.int})" | ||
| reveal_type(Sneaky(f5, 1).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y'?: builtins.int})" | ||
| reveal_type(Sneaky(f5, 1, 2).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y'?: builtins.int})" | ||
| reveal_type(Sneaky(f6, x=1).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | ||
| reveal_type(Sneaky(f6, x=1, y=2).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" | ||
| reveal_type(Sneaky(f7, 1, y='').kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int})" | ||
| reveal_type(Sneaky(f8, 1, y='').kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.str]" | ||
| reveal_type(Sneaky(f9, 1, y=0).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y': builtins.int, 'z'?: builtins.str})" | ||
| reveal_type(Sneaky(f9, 1, y=0, z='').kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y': builtins.int, 'z'?: builtins.str})" | ||
| [builtins fixtures/paramspec.pyi] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.