-
-
Notifications
You must be signed in to change notification settings - Fork 3k
More helpful type guards #14238
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
Merged
Merged
More helpful type guards #14238
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a29a6ec
The specification requires positional arguments
A5rocks 3dda329
Start allowing keyword arguments to type guards
A5rocks 10c7e3f
Special case staticmethod
A5rocks 3ea4baf
PR review
A5rocks 9c9a791
Fix tests
A5rocks 43adc46
Small test fixes
A5rocks 58af039
Merge branch 'master' into typeguard-ocd
A5rocks c6f55d1
Move Python 3.8 tests to check-python38
A5rocks e7265c4
Merge branch 'master' into typeguard-ocd
hauntsaninja 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
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
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 |
---|---|---|
|
@@ -37,8 +37,8 @@ reveal_type(foo) # N: Revealed type is "def (a: builtins.object) -> TypeGuard[b | |
[case testTypeGuardCallArgsNone] | ||
from typing_extensions import TypeGuard | ||
class Point: pass | ||
# TODO: error on the 'def' line (insufficient args for type guard) | ||
def is_point() -> TypeGuard[Point]: pass | ||
|
||
def is_point() -> TypeGuard[Point]: pass # E: TypeGuard functions must have a positional argument | ||
def main(a: object) -> None: | ||
if is_point(): | ||
reveal_type(a) # N: Revealed type is "builtins.object" | ||
|
@@ -227,13 +227,13 @@ def main(a: object) -> None: | |
from typing_extensions import TypeGuard | ||
def is_float(a: object, b: object = 0) -> TypeGuard[float]: pass | ||
def main1(a: object) -> None: | ||
# This is debatable -- should we support these cases? | ||
if is_float(a=a, b=1): | ||
reveal_type(a) # N: Revealed type is "builtins.float" | ||
|
||
if is_float(a=a, b=1): # E: Type guard requires positional argument | ||
reveal_type(a) # N: Revealed type is "builtins.object" | ||
if is_float(b=1, a=a): | ||
reveal_type(a) # N: Revealed type is "builtins.float" | ||
|
||
if is_float(b=1, a=a): # E: Type guard requires positional argument | ||
reveal_type(a) # N: Revealed type is "builtins.object" | ||
# This is debatable -- should we support these cases? | ||
|
||
ta = (a,) | ||
if is_float(*ta): # E: Type guard requires positional argument | ||
|
@@ -597,3 +597,77 @@ def func(names: Tuple[str, ...]): | |
if is_two_element_tuple(names): | ||
reveal_type(names) # N: Revealed type is "Tuple[builtins.str, builtins.str]" | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testTypeGuardErroneousDefinitionFails] | ||
from typing_extensions import TypeGuard | ||
|
||
class Z: | ||
def typeguard(self, *, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument | ||
... | ||
|
||
def bad_typeguard(*, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument | ||
... | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testTypeGuardWithKeywordArg] | ||
from typing_extensions import TypeGuard | ||
|
||
class Z: | ||
def typeguard(self, x: object) -> TypeGuard[int]: | ||
... | ||
|
||
def typeguard(x: object) -> TypeGuard[int]: | ||
... | ||
|
||
n: object | ||
if typeguard(x=n): | ||
reveal_type(n) # N: Revealed type is "builtins.int" | ||
|
||
if Z().typeguard(x=n): | ||
reveal_type(n) # N: Revealed type is "builtins.int" | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testStaticMethodTypeGuard] | ||
from typing_extensions import TypeGuard | ||
|
||
class Y: | ||
@staticmethod | ||
def typeguard(h: object) -> TypeGuard[int]: | ||
... | ||
|
||
x: object | ||
if Y().typeguard(x): | ||
reveal_type(x) # N: Revealed type is "builtins.int" | ||
if Y.typeguard(x): | ||
reveal_type(x) # N: Revealed type is "builtins.int" | ||
[builtins fixtures/tuple.pyi] | ||
[builtins fixtures/classmethod.pyi] | ||
|
||
[case testTypeGuardKwargFollowingThroughOverloaded] | ||
from typing import overload, Union | ||
from typing_extensions import TypeGuard | ||
|
||
@overload | ||
def typeguard(x: object, y: str) -> TypeGuard[str]: | ||
... | ||
|
||
@overload | ||
def typeguard(x: object, y: int) -> TypeGuard[int]: | ||
... | ||
|
||
def typeguard(x: object, y: Union[int, str]) -> Union[TypeGuard[int], TypeGuard[str]]: | ||
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. Not sure this return type makes sense. But can add an error in another PR |
||
... | ||
|
||
x: object | ||
if typeguard(x=x, y=42): | ||
reveal_type(x) # N: Revealed type is "builtins.int" | ||
|
||
if typeguard(y=42, x=x): | ||
reveal_type(x) # N: Revealed type is "builtins.int" | ||
|
||
if typeguard(x=x, y="42"): | ||
reveal_type(x) # N: Revealed type is "builtins.str" | ||
|
||
if typeguard(y="42", x=x): | ||
reveal_type(x) # N: Revealed type is "builtins.str" | ||
[builtins fixtures/tuple.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.