-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[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
[ty] Add attribute assignment tests #18527
Conversation
| # instead of unresolved-attribute, this should report possibly-unbound-attribute | ||
| # TODO: error: [possibly-unbound-attribute] | ||
| # error: [unresolved-attribute] | ||
| a.x = 42 |
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.
mypy:
../test.py:12: error: Item "A" of "A | B" has no attribute "x" [union-attr]
ce040d2 to
ca79111
Compare
|
4cf497e to
a74c196
Compare
| class B: | ||
| x: int | ||
|
|
||
| C = TypeVar("C", bound=Union[A, B]) |
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 regression this doesn't catch the issue/fail on the other branch
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.
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: Attribute callbackon typeBeartypeableT is possibly unbound) is no longer being emitted on #18347
however while investigating i did find what i think is an issue on main with setting attributes on generics where the upper bound is a union, after narrowing. added a test to illustrate
this results in a possibly-unbound-attribute from ty but no diagnostic from mypy
mypy
../test.py:16: note: Revealed type is "test.B"
Success: no issues found in 1 source file
ty
info[revealed-type]: Revealed type
--> /Users/justinchapman/src/test.py:16:23
|
14 | if not isinstance(b, B):
15 | raise TypeError("Expected instance of B")
16 | print(reveal_type(b))
| ^ `C & B`
17 | b.x = 42
|
code
from typing import Union, TypeVar
from typing_extensions import reveal_type
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")
print(reveal_type(b))
b.x = 42
b = B()
_(b)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.
a74c196 to
ea2d263
Compare
| a.x = 42 # error: [possibly-unbound-attribute] | ||
| ``` | ||
|
|
||
| ### Assigning to a data descriptor attribute |
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 datadog regression doesn't seem to
ea2d263 to
5f6f995
Compare
5f6f995 to
933a21e
Compare
Summary
Related to: #18347
This PR adds tests for some of the current behavior of
validate_attribute_assignment, to help with the refactor. This catches some issues that surfaced in the ecosystem check.Opening as a separate PR so I can see these pass against
mainthen rebase #18347 on top of this and fail themProjects
Test Plan
New mdtests