-
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
Implement PEP 604 rewrites for isinstance
and issubclass
checks
#2923
Comments
Not that it affects the usefulness of such a rule, but note that
This is on Python 3.10, might be faster on newer versions. |
Interesting! Definitely worth putting a note beside the rule. |
python 3.11 has optimizations related to exactly this; see python/cpython#91603 on python 3.10: $ a='isinstance("", (int, str))'
$ b='isinstance("", int | str)'
$ python3.10 -m timeit "$a"; python3.10 -m timeit "$b"
5000000 loops, best of 5: 48.6 nsec per loop
2000000 loops, best of 5: 108 nsec per loop python 3.11 is significantly faster in both cases, and the difference between the two approaches has shrunk: $ python3.11 -m timeit "$a"; python3.11 -m timeit "$b"
5000000 loops, best of 5: 40.6 nsec per loop
5000000 loops, best of 5: 63.2 nsec per loop note that |
Can I take a look at this one? |
Sure! We can probably make it part of |
@charliermarsh I'd be in favor of separating it out into it's on rule code as one could imagine stylistically a user preferring this syntax, but not like replacing Union with |
@Skylion007 - I'm a little torn because that rule is intended to capture PEP 604 adoption / usage, and this behavior is explicitly part of PEP 604. |
It's been a while since I contributed last, so I don't remember everything. How would this interact with SIM101 rewriting |
It's okay to "ignore" that, since if both rules are enabled, we'd end up correcting |
I have a first draft, but not sure about code architecture: it doesn't to fit in the same function / ast level |
This was added in #3280. |
PEP 604 introduced simplified syntax for union types e.g.
var: str | int
for which we already have support for in #369. It also allows this syntax to be used insideisinstance
andissubclass
checks, it would be great if these could be autofixed too!The text was updated successfully, but these errors were encountered: