asottile / pyupgrade

A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language.
MIT License
3.61k stars 184 forks source link

`Generator` type annotation wrongly shortened #962

Closed david-zwicker closed 3 months ago

david-zwicker commented 3 months ago

pyupgrade turns the following code

def f() -> Generator[float, None, None]:
    ...

into

def f() -> Generator[float]:
    ...

which is wrong according to mypy. I believe pyupgrade should keep the three-argument version.

asottile commented 3 months ago

you're on an old mypy

david-zwicker commented 3 months ago

I'm on mypy 1.11.1 (compiled: yes), which I believe is the latest one.

asottile commented 3 months ago

show your output and full code?

david-zwicker commented 3 months ago

I cannot fully isolate the problem, but it appears in the code base of py-pde. I'll try to come up with a minimal example and post it here!

asottile commented 3 months ago

please start with that next time instead of wasting my time!

e-gebes commented 2 months ago

I have the same or a similar issue. I ran pyupgrade --py310-plus myfile.py on the following code

from __future__ import annotations

from collections.abc import Generator

def generator() -> Generator[int, None, None]:
    yield 1

and it gets shortened to:

from __future__ import annotations

from collections.abc import Generator

def generator() -> Generator[int]:
    yield 1

My version of pyupgrade is 3.17.0

Without the from __future__ import annotations line the Generator type hint is not shortened.

I think

asottile commented 2 months ago

you're wrong. mypy accepts it just fine with the import just like all other improvements.

where are you seeing otherwise?

e-gebes commented 2 months ago

it's irrelevant if a code optimization works with one specific tool read the CPython doc!

it's legitimate to write Python 3.10 code, it's legitimate to write 3.10 code with the annotations import, and it's legitimate to write the type hints in the same 3.10-way

when I run pyupgrade --py310-plus apparently the "3.10 type hints" are upgraded to 3.13 type hints (!), which is surprising behavior - it'd be better to change that

at least the behavior is somehow documented: https://github.com/asottile/pyupgrade?tab=readme-ov-file#pep-696-typevar-defaults and there's remedy with --keep-runtime-typing in this case

asottile commented 2 months ago

read the pyupgrade doc!

you still haven't demonstrated any breakage so I'm assuming you're just a troll