Skip to content
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

Do not upgrade string annotations types wrapped in Optional to new Union | formatting #567

Closed
wakemaster39 opened this issue Nov 15, 2021 · 7 comments · Fixed by #568
Closed
Labels

Comments

@wakemaster39
Copy link

wakemaster39 commented Nov 15, 2021

Simplistic python code:

qqq: "int" | None = 0

Output:

python qqq.py
Traceback (most recent call last):
  File "qqq.py", line 2, in <module>
    qqq: "int" | None = 0
TypeError: unsupported operand type(s) for |: 'str' and 'NoneType'

When running pyupgrade a segment of code was updated to use the | notation and caused this explosion. I worked around it, but I am sure others might run into similar issues.

@asottile
Copy link
Owner

you've either opted into a python version that's newer than the one you're running or something else you're not showing -- pyupgrade only does this if it's safe

@wakemaster39
Copy link
Author

@asottile i am running latest python 3.10

@asottile
Copy link
Owner

well then there's something you're not showing me -- watch this video and try again: https://youtu.be/ritp4gAqNMI

@wakemaster39
Copy link
Author

wakemaster39 commented Nov 15, 2021

you are right, I didn't finish the bug report. Sorry mac pressed submit. Not use to a github repo getting such quick attention, which is awesome btw. Here is the rest you need to replicate the issue fully.

from typing import Optional

from sqlalchemy.orm import Mapped, relationship

qqq: Mapped[Optional["WorkspaceModel"]]

After running pyupgrade, the output I am getting is:

from sqlalchemy.orm import Mapped

qqq: Mapped["WorkspaceModel" | None]

@asottile
Copy link
Owner

pyupgrade does not change that for for me, please watch the video and follow what it asks you to do

@wakemaster39
Copy link
Author

wakemaster39 commented Nov 15, 2021

  1. I am running pyupgrade using pre-commit hooks to keep my code based with the latest syntax
  2. I have the following in my pre-commit hooks file:
  - repo: https://github.com/asottile/pyupgrade
    rev: v2.29.0
    hooks:
      - id: pyupgrade
        args:
          - --py310-plus

When running a pre-commt run --all on a file (qqq.py) with a file containing the content of:

from typing import Optional

from sqlalchemy.orm import Mapped

qqq: Mapped[Optional["WorkspaceModel"]]
  1. I would expect the file to have no changes written to it.

  2. What is happening is that a file with the following content is written:

from sqlalchemy.orm import Mapped

qqq: Mapped["WorkspaceModel" | None]

As an additional note, the output does not change if I use pyupgrade qqq.py --py310-plus instead of using the pre-commit hook.

@asottile
Copy link
Owner

there were go, that's much more helpful and now I can reproduce

you can likely work around this with from __future__ import annotations until this is fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants