-
Notifications
You must be signed in to change notification settings - Fork 21
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
use a generic's bound when narrowing with isinstance
if it's covariant, or Never
if it's contravariant
#745
Draft
DetachHead
wants to merge
6
commits into
main
Choose a base branch
from
isinstance-narrow-generics-to-bound
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
81412b2
use a generic's bound when narrowing with `isinstance` if it's covari…
DetachHead 5ea940f
do the same with match statements
DetachHead ec6055e
add tests for typevar narrowing changes
DetachHead 69fb6f1
don't do the variance narrowing thing if the type was originally `Any…
DetachHead d5ef6a7
workaround for fake typevars in `__init__` methods when determining w…
DetachHead 82d3320
fail
DetachHead 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 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
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
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
72 changes: 72 additions & 0 deletions
72
packages/pyright-internal/src/tests/samples/typeNarrowingUsingBounds.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import Any, Never, assert_type, Iterable, Iterator, MutableMapping, Reversible | ||
|
||
|
||
class Covariant[T]: | ||
def foo(self, other: object): | ||
if isinstance(other, Covariant): | ||
assert_type(other, Covariant[object]) | ||
|
||
def bar(self) -> T: ... | ||
|
||
class CovariantByDefault[T]: | ||
"""by default if there are no usages of a type var on a class, it's treated as covariant. | ||
imo this should be an error. see https://github.com/DetachHead/basedpyright/issues/744""" | ||
def foo(self, other: object): | ||
if isinstance(other, CovariantByDefault): | ||
assert_type(other, CovariantByDefault[object]) | ||
|
||
class CovariantWithBound[T: int | str]: | ||
def foo(self, other: object): | ||
if isinstance(other, CovariantWithBound): | ||
assert_type(other, CovariantWithBound[int | str]) | ||
|
||
def bar(self) -> T: ... | ||
|
||
class Contravariant[T]: | ||
def foo(self, other: object): | ||
if isinstance(other, Contravariant): | ||
assert_type(other, Contravariant[Never]) | ||
|
||
def bar(self, other: T): ... | ||
|
||
class ContravariantWithBound[T: int | str]: | ||
def foo(self, other: object): | ||
if isinstance(other, ContravariantWithBound): | ||
assert_type(other, ContravariantWithBound[Never]) | ||
|
||
def bar(self, other: T): ... | ||
|
||
|
||
def foo(value: object): | ||
match value: | ||
case Iterable(): | ||
assert_type(value, Iterable[object]) | ||
|
||
class AnyOrUnknown: | ||
"""for backwards compatibility with badly typed code we keep the old functionality when narrowing `Any`/Unknown""" | ||
def __init__(self, value): | ||
"""arguments in `__init__` get turned into fake type vars if they're untyped, so we need to handle this case. | ||
see https://github.com/DetachHead/basedpyright/issues/746""" | ||
if isinstance(value, Iterable): | ||
assert_type(value, Iterable[Any]) | ||
|
||
def any(self, value: Any): | ||
if isinstance(value, Iterable): | ||
assert_type(value, Iterable[Any]) | ||
|
||
def match_case(self, value: Any): | ||
match value: | ||
case Iterable(): | ||
assert_type(value, Iterable[Any]) | ||
|
||
def unknown(self, value): | ||
if isinstance(value, Iterable): | ||
assert_type(value, Iterable[Any]) | ||
|
||
def partially_unknown(self, value=None): | ||
if isinstance(value, Iterable): | ||
assert_type(value, Iterable[Any]) | ||
|
||
def foo[KT,VT](self: MutableMapping[KT, VT]) -> Iterator[KT]: | ||
assert isinstance(self, Reversible) # fail | ||
return reversed(self) |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem like a good change. If we already have a narrower type var, it seems like it shouldn't widen it.
Do you know an example where narrowing to
ChildB1[int]
would be unsafe?If it's just an issue of being difficult to implement, I think this change probably isn't bad enough to be a blocker.