Skip to content
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

Implement TypeIs (PEP 742) #16898

Merged
merged 30 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8a30073
Basic work on TypeNarrower
JelleZijlstra Feb 9, 2024
58e8403
Initial tests (many failing)
JelleZijlstra Feb 9, 2024
c8d2af8
Fewer test failures
JelleZijlstra Feb 10, 2024
f205910
Fix the remaining tests
JelleZijlstra Feb 10, 2024
75c9dec
Did not actually need TypeNarrowerType
JelleZijlstra Feb 10, 2024
4666486
Error for bad narrowing
JelleZijlstra Feb 10, 2024
25a9c79
temp change typeshed
JelleZijlstra Feb 10, 2024
faa4a07
Fixes
JelleZijlstra Feb 10, 2024
c0e0210
Merge branch 'master' into typenarrower
JelleZijlstra Feb 10, 2024
f107e5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2024
34700bb
doc
JelleZijlstra Feb 10, 2024
065ec92
fix self check
JelleZijlstra Feb 10, 2024
aef3036
like this maybe
JelleZijlstra Feb 10, 2024
4b19c77
Merge remote-tracking branch 'upstream/master' into typenarrower
JelleZijlstra Feb 10, 2024
6b0e749
Fix and add tests
JelleZijlstra Feb 10, 2024
c9e53e6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2024
909e53c
Use TypeIs
JelleZijlstra Feb 14, 2024
eb88371
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 14, 2024
1b1e368
Apply suggestions from code review
JelleZijlstra Feb 22, 2024
84c69d2
Code review feedback, new test case, fix incorrect constraints
JelleZijlstra Feb 23, 2024
ae294bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 23, 2024
7fedbcf
Merge remote-tracking branch 'upstream/master' into typenarrower
JelleZijlstra Mar 1, 2024
dbc229d
Rename error code
JelleZijlstra Mar 1, 2024
8b2fb0b
Quote name
JelleZijlstra Mar 1, 2024
816fd1a
unxfail
JelleZijlstra Mar 1, 2024
d6fcc35
add elif test
JelleZijlstra Mar 1, 2024
ef825ce
type context test
JelleZijlstra Mar 1, 2024
d32956d
Add test
JelleZijlstra Mar 1, 2024
a36a16a
Add error code test case
JelleZijlstra Mar 1, 2024
b32ba80
update docs
JelleZijlstra Mar 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 10, 2024
commit f107e5b01bcf7b4592c848d954fce8c7444e5e63
5 changes: 1 addition & 4 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,10 +1451,7 @@ def check_call_expr_with_callee_type(
object_type=object_type,
)
proper_callee = get_proper_type(callee_type)
if (
isinstance(e.callee, RefExpr)
and isinstance(proper_callee, CallableType)
):
if isinstance(e.callee, RefExpr) and isinstance(proper_callee, CallableType):
# Cache it for find_isinstance_check()
if proper_callee.type_guard is not None:
e.callee.type_guard = proper_callee.type_guard
Expand Down
4 changes: 3 additions & 1 deletion mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ def visit_callable_type(self, t: CallableType) -> CallableType:
arg_names=t.arg_names[:-2] + repl.arg_names,
ret_type=t.ret_type.accept(self),
type_guard=(t.type_guard.accept(self) if t.type_guard is not None else None),
type_narrower=(t.type_narrower.accept(self) if t.type_narrower is not None else None),
type_narrower=(
t.type_narrower.accept(self) if t.type_narrower is not None else None
),
imprecise_arg_kinds=(t.imprecise_arg_kinds or repl.imprecise_arg_kinds),
variables=[*repl.variables, *t.variables],
)
Expand Down
4 changes: 3 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,9 @@ def visit_callable_type(self, left: CallableType) -> bool:
# a TypeNarrower[Child] when a TypeNarrower[Parent] is expected, because
# if the narrower returns False, we assume that the narrowed value is
# *not* a Parent.
if not self._is_subtype(left.type_narrower, right.type_narrower) or not self._is_subtype(right.type_narrower, left.type_narrower):
if not self._is_subtype(
left.type_narrower, right.type_narrower
) or not self._is_subtype(right.type_narrower, left.type_narrower):
return False
elif right.type_guard is not None and left.type_guard is None:
# This means that one function has `TypeGuard` and other does not.
Expand Down
5 changes: 4 additions & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,10 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
)
return AnyType(TypeOfAny.from_error)
return RequiredType(self.anal_type(t.args[0]), required=False)
elif self.anal_type_guard_arg(t, fullname) is not None or self.anal_type_narrower_arg(t, fullname) is not None:
elif (
self.anal_type_guard_arg(t, fullname) is not None
or self.anal_type_narrower_arg(t, fullname) is not None
):
# In most contexts, TypeGuard[...] acts as an alias for bool (ignoring its args)
return self.named_type("builtins.bool")
elif fullname in ("typing.Unpack", "typing_extensions.Unpack"):
Expand Down
8 changes: 6 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,9 @@ def serialize(self) -> JsonDict:
"bound_args": [(None if t is None else t.serialize()) for t in self.bound_args],
"def_extras": dict(self.def_extras),
"type_guard": self.type_guard.serialize() if self.type_guard is not None else None,
"type_narrower": self.type_narrower.serialize() if self.type_narrower is not None else None,
"type_narrower": (
self.type_narrower.serialize() if self.type_narrower is not None else None
),
"from_concatenate": self.from_concatenate,
"imprecise_arg_kinds": self.imprecise_arg_kinds,
"unpack_kwargs": self.unpack_kwargs,
Expand All @@ -2255,7 +2257,9 @@ def deserialize(cls, data: JsonDict) -> CallableType:
deserialize_type(data["type_guard"]) if data["type_guard"] is not None else None
),
type_narrower=(
deserialize_type(data["type_narrower"]) if data["type_narrower"] is not None else None
deserialize_type(data["type_narrower"])
if data["type_narrower"] is not None
else None
),
from_concatenate=data["from_concatenate"],
imprecise_arg_kinds=data["imprecise_arg_kinds"],
Expand Down
Loading