Open walter9388 opened 1 month ago
this is pretty difficult to do in a single pass but should be somewhat doable
something similar is done for except blocks already if you want to try and tackle this for type unions as well
Ok nice, I just had a very quick look and can see where this happens for except blocks. I will review when I have a bit more time in the next few days and submit a PR when I can :) Cheers
This is a bit niche, but when I was reviewing some naive code submitted to me I saw some bizarre typing generated by
pyupgrade
which looked something likewhere
None
is duplicated.When looking into how this had happened, I noticed it came from the following:
Clearly wrapping
Union[..., None]
withOptional
is bad, but it made me wonder which dev tool can detect this sort of issue. It doesn't seem to be something that is picked up bymypy
,flake8
,pyright
, etc. (Just to clarify this bad typing had been generated by an internal tool which wraps given types withOptional
for PATCH methods in a RESTful API, so it is very much an edge case)Do you think deduplication of types is something that can live in the PEP604 part of pyupgrade? I think it is unlikely that anyone would write
int | None | None
manually, but I can see when over complicating unions this sort of thing might happen.I added the following test to
test_fix_pep604_types
just to check this was expected behaviour:and sure enough pytest failed with the following duplicated types:
I'd be interested to hear your opinions on this. If you do think it would be a complimentary feature for
pyupgrade
then I'm happy to submit a PR for consideration to get the test above passing.