asottile / pyupgrade

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

type error when change "Union" operator to "|" operator #925

Closed MSafariyan closed 8 months ago

MSafariyan commented 8 months ago
requires-python = ">=3.11"
  - repo: https://github.com/asottile/pyupgrade
    rev: v3.15.0
    hooks:
      - id: pyupgrade
        args:
          - --py311-plus

currently i have a similar method:

from collections.abc import Awaitable

class RedisPort:
    @abstractmethod
-   def srem(self, name: str, *values: bytes | str | float) -> Union[Awaitable[int], int]:
+   def srem(self, name: str, *values: bytes | str | float) -> Awaitable[int] | int:
        raise NotImplementedError

then pyupgrade try to change Union operator to | so the python raise TypeError exception:

(<class 'TypeError'>, unsupported operand type(s) for |: 'types.GenericAlias' and 'function', <traceback object at 0x7f2a3223f080>)

asottile commented 8 months ago

when you instruct pyupgrade to --py311-plus you're telling it "I only run python 3.11 or newer" -- your error message is from python 3.9