-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Add attribute assignment tests #18527
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
Closed
thejchap
wants to merge
1
commit into
astral-sh:main
from
thejchap:thejchap/union-attribute-assignment-possibly-undefined
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1685,6 +1685,125 @@ date.year = 2025 | |
| date.tz = "UTC" | ||
| ``` | ||
|
|
||
| ### Setting attributes on unions | ||
|
|
||
| Setting attributes on unions where all elements of the union have the attribute is acceptable | ||
|
|
||
| ```py | ||
| from typing import Union | ||
|
|
||
| class A: | ||
| x: int | ||
|
|
||
| class B: | ||
| x: int | ||
|
|
||
| C = Union[A, B] | ||
|
|
||
| a: C = A() | ||
| a.x = 42 | ||
| ``` | ||
|
|
||
| Setting attributes on unions where any element of the union does not have the attribute reports | ||
| possibly unbound | ||
|
|
||
| ```py | ||
| from typing import Union, Sequence | ||
|
|
||
| class A: | ||
| pass | ||
|
|
||
| class B: | ||
| x: int | ||
|
|
||
| C = Union[A, B] | ||
|
|
||
| def _(a: C): | ||
| a.x = 42 # TODO: error: [possibly-unbound-attribute] | ||
| ``` | ||
|
|
||
| The same goes for | ||
|
|
||
| ```py | ||
| from dataclasses import dataclass | ||
| from typing import Union, Sequence | ||
| from abc import ABC | ||
|
|
||
| class Base(ABC): | ||
| x: Sequence[bytes] = () | ||
|
|
||
| class Derived(Base): | ||
| pass | ||
|
|
||
| class Other: | ||
| pass | ||
|
|
||
| D = Union[Derived, Other] | ||
|
|
||
| d: D = Other() | ||
|
|
||
| # TODO: error: [possibly-unbound-attribute] | ||
| # error: [unresolved-attribute] | ||
| d.x = None | ||
| ``` | ||
|
|
||
| When setting an attribute on a generic type where the upper bound is a union, after narrowing, an | ||
| attribute only present on the narrowed type should be able to be set: | ||
|
|
||
| ```py | ||
| from typing import Union, TypeVar | ||
|
|
||
| class A: | ||
| pass | ||
|
|
||
| class B: | ||
| def __init__(self) -> None: | ||
| self.x: int = 0 | ||
|
|
||
| C = TypeVar("C", bound=Union[A, B]) | ||
|
|
||
| def _(b: C): | ||
| if not isinstance(b, B): | ||
| raise TypeError("Expected instance of B") | ||
| reveal_type(b) # revealed: B | ||
| b.x = 42 | ||
| ``` | ||
|
|
||
| Setting attributes on a generic where the upper bound is a union, and not all elements of the union | ||
| have the attribute, also reports possibly unbound: | ||
|
|
||
| ```py | ||
| from typing import Union, TypeVar | ||
|
|
||
| class A: | ||
| pass | ||
|
|
||
| class B: | ||
| x: int | ||
|
|
||
| C = TypeVar("C", bound=Union[A, B]) | ||
|
|
||
| def _(a: C): | ||
| a.x = 42 # error: [possibly-unbound-attribute] | ||
| ``` | ||
|
|
||
| ### Assigning to a data descriptor attribute | ||
|
Contributor
Author
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.
|
||
|
|
||
| This is invalid | ||
|
|
||
| ```py | ||
| class A: | ||
| def __init__(self, x: int): | ||
| self.x = x | ||
|
|
||
| @property | ||
| def y(self) -> int: | ||
| return self.x | ||
|
|
||
| a = A(42) | ||
| a.y = 1 # error: [invalid-assignment] "Invalid assignment to data descriptor attribute `y` on type `A` with custom `__set__` method" | ||
| ``` | ||
|
|
||
| ### `argparse.Namespace` | ||
|
|
||
| A standard library example of a class with a custom `__setattr__` method is `argparse.Namespace`: | ||
|
|
||
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.
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 should address the beartype regressionthis doesn't catch the issue/fail on the other branchThere 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.
@sharkdp
making slow progress on these - limited time :)
i'm still unclear on why the warning in beartype (
warning[possibly-unbound-attribute] beartype/_util/api/external/utilclick.py:108:5: Attributecallbackon typeBeartypeableTis possibly unbound) is no longer being emitted on #18347however while investigating i did find what i think is an issue on
mainwith setting attributes on generics where the upper bound is a union, after narrowing. added a test to illustratethis results in a
possibly-unbound-attributefromtybut no diagnostic frommypymypy
ty
code
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.
That looks like a bug, yes. A similar bug was mentioned on Discord the other day. Would you mind reporting that as a separate issue? A simplified example would be https://play.ty.dev/62d91bdb-cbf1-4758-8f2c-6a26722c9f47
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.
@sharkdp astral-sh/ty#653