-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix invalid type false positive #8206
Changes from 12 commits
e0d32da
4741057
07d595e
8e42987
dc8f72b
bad4805
95cfe9f
e4df864
e6b70c2
3d0ebe7
1c62667
673fa05
bb38e7c
66d21ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fix false positive for isinstance-second-argument-not-valid-type with union types. | ||
|
||
Closes #8205 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'''Tests for invalid isinstance with compound types''' | ||
|
||
# True negatives | ||
isinstance(0, int | str) | ||
isinstance(0, int | int | int) | ||
isinstance(0, int | str | list | float) | ||
isinstance(0, (int | str) | (list | float)) | ||
|
||
IntOrStr = int | str | ||
isinstance(0, IntOrStr) | ||
ListOrDict = list | dict | ||
isinstance(0, (float | ListOrDict) | IntOrStr) | ||
|
||
# True positives | ||
isinstance(0, int | 5) # [isinstance-second-argument-not-valid-type] | ||
isinstance(0, str | 5 | int) # [isinstance-second-argument-not-valid-type] | ||
INT = 5 | ||
isinstance(0, INT | int) # [isinstance-second-argument-not-valid-type] | ||
|
||
|
||
# FALSE NEGATIVES | ||
Pierre-Sassoulas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Parameterized generics will raise type errors at runtime. | ||
# Warnings should be raised, but aren't (yet). | ||
isinstance(0, list[int]) | ||
ListOfInts = list[int] | ||
isinstance(0, ListOfInts) |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,2 @@ | ||||||||||||||||
[testoptions] | ||||||||||||||||
min_pyver=3.10 | ||||||||||||||||
Comment on lines
+1
to
+2
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. maybe you could allow the test to run also for earlier versions like was done in
Suggested change
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. What does the 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. Now that I think about it, this check is different because it doesn't deal with type annotations. In this case the types involved will actually be called, so I think this option won't apply. 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. you don't have to explicitly write
I think you are right. These are not annotations it the actual code. so if someone uses the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
isinstance-second-argument-not-valid-type:15:0:15:22::Second argument of isinstance is not a type:UNDEFINED | ||
isinstance-second-argument-not-valid-type:16:0:16:28::Second argument of isinstance is not a type:UNDEFINED | ||
isinstance-second-argument-not-valid-type:18:0:18:24::Second argument of isinstance is not a type:UNDEFINED |
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.
Is the
bases.UnionType
the legacy Union/Optional type? if so it is supported from 3.7+ I think.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.
No,
Union[int, str]
is an instance ofClassDef
and notUnionType
. I'm not sure what exactlyUnionType
corresponds to.