question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Presence of future annotation import causes improper application of PEP 604 for Python 3.7

See original GitHub issue

Given these file contents:

# foo.py

from __future__ import annotations

from typing import Optional


def some_function(some_arg: Optional[str] = None):
    ...

Running the following command:

$ pyupgrade --py37-plus

Rewrites the file contents to:

# foo.py

from __future__ import annotations

from typing import Optional


def some_function(some_arg: str | None = None):
    ...

Which to my understanding is not valid before Python 3.10. Note that if the from __future__ import annotations line is not present initially, the file remains unchanged.

This is the case on at least versions 2.29.1 and 2.31.0.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, Jan 4, 2022

It isn’t valid in Python 3.7 regardless

it is valid if you have from __future__ import annotations:

(venv37) marcogorelli@OVMG025 tmp % cat t.py 
# from __future__ import annotations
a: str | None
(venv37) marcogorelli@OVMG025 tmp % python3.7 t.py 
Traceback (most recent call last):
  File "t.py", line 2, in <module>
    a: str | None
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
(venv37) marcogorelli@OVMG025 tmp % vim t.py 
(venv37) marcogorelli@OVMG025 tmp % cat t.py 
from __future__ import annotations
a: str | None
(venv37) marcogorelli@OVMG025 tmp % python3.7 t.py

I took these listed conditions to be ANDed together, but seems instead to behave as an OR currently.

That’s right, it’s OR. I think this was brought up before but the maintainer thought it was clear enough as it is

0reactions
asottilecommented, Jan 12, 2022

you don’t respect it though, you’re here making noise and telling people how to use the tool

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support PEP 604 -- Allow writing union types as X | Y
Properly take into account the presence of from __future__ import annotations . Ensure that the warnings about the syntax remain in earlier versions...
Read more >
What, within __future__, made list[str] start working on 3.7?
I understand that PEP 585 lets us type hint the standard collection objects ... from __future__ import annotations x: list[str] = ["a", "b"]....
Read more >
Python 3.7: from __future__ import annotations #2950 - GitHub
Hi, I'm on Windows 10, Python 3.7.1, Cython 0.29.7. I have some pure python code that I wanted to compile that uses from...
Read more >
Mypy Documentation
Mypy is a static type checker for Python. Type checkers help ensure that you're using variables and functions in your code correctly.
Read more >
Can't import annotations from __future__ - Stack Overflow
If so, then the error happens because according to PEP-563 the import of __future__ annotations is available starting with Python 3.7 .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found