asottile / pyupgrade

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

PEP 585 rewrites don't work #978

Closed phormio closed 1 month ago

phormio commented 1 month ago
bash$ cat -n experiment1.py
     1  from __future__ import annotations
     2  
     3  def f(x: List[str]) -> None
     4      print(100)
bash$ python3 --version
Python 3.11.2
bash$ pipx run pyupgrade --py39-plus experiment1.py
bash$ cat -n experiment1.py
     1  from __future__ import annotations
     2  
     3  def f(x: List[str]) -> None
     4      print(100)
bash$ 

pyupgrade should change List[str] to list[str]. In fact, it leaves the file unchanged.

asottile commented 1 month ago

garbage in garbage out. your code has a NameError

phormio commented 1 month ago

It has a SyntaxError. When I fix it, there is no NameError.

Here is the corrected program:

from __future__ import annotations

from typing import List

def f(x: List[str]) -> None:
    print(100)

The pyupgrade command in my previous comment now behaves according to the documentation.

I wrote the program to check whether pyupgrade requires all of the list items in the "Availability" section to be true or only one of them. The documentation is unclear on this point.

asottile commented 1 month ago

why would you expect a malformed input to produce a meaningful result

I don't understand the mental gymnastics