[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.
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.
this should address the beartype regression this doesn't catch the issue/fail on the other branch
There was a problem hiding this comment.
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.
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.
a74c196 to
ea2d263
Compare
| a.x = 42 # error: [possibly-unbound-attribute] | ||
| ``` | ||
|
|
||
| ### Assigning to a data descriptor attribute |
There was a problem hiding this comment.
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