typer
typer copied to clipboard
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the Typer documentation, with the integrated search.
- [X] I already searched in Google "How to X in Typer" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to Typer but to Click.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
from __future__ import annotations
import typer
app = typer.Typer()
def main():
app()
@app.command()
def test(flag: bool | None = typer.Option(None, "--flag", "-f", help="A flag")):
print(flag)
if __name__ == "__main__":
main()
Description
- Run the example code
- See the error: https://sourceb.in/SxyHEeLZxf
Operating System
Linux
Operating System Details
No response
Typer Version
0.4.0
Python Version
Python 3.8.10
Additional Context
No response
|
was added in python 3.10, so not available in python 3.8.
def test(flag: bool | None = typer.Option(None, "--flag", "-f", help="A flag")):
Either you can use typing.Union
or you can use string for type annotation, like:
def test(flag: "bool | None" = typer.Option(None, "--flag", "-f", help="A flag")):
|
was added in python 3.10, so not available in python 3.8.def test(flag: bool | None = typer.Option(None, "--flag", "-f", help="A flag")):
Either you can use
typing.Union
or you can use string for type annotation, like:def test(flag: "bool | None" = typer.Option(None, "--flag", "-f", help="A flag")):
from __future__ import annotations
I've imported it from __future__
though.
Also, this sample code works in python 3.8, as I've imported annotations
from __future__ import annotations
def test(arg: int | str) -> None:
print(arg)
test("string")
test(999)
I'm getting the same issue with python 3.10:
RuntimeError: Type not yet supported: str | None
def create(self,
major: int|str, minor: int|str, patch: int|str,
prerelease: str, buildmetadata: str) -> 'Version':
pass
This causes TypeError: unsupported operand type(s) for |: 'type' and 'type'
when I import this file into ipython, but not when I run this file from the command line or start a python session from the command line and import the file that way.
~~Python version 3.10.4~~
Turns out my iPython was pointing to wrong python version (3.7.10) - Needed to point it to 3.10
Would the typealias
String = str | None
be a possibility? It should match the semantics. The other types (Float, ...) could also be turned into aliases.
Though, Typealias seems to require python 3.10 (https://peps.python.org/pep-0613/). Requiring 3.10 would be no issue for me as I'm already developing with it.
Is supporting < 3.10 a hard requirement for this module?
Duplicate of
- #348
Confirmed that either #676 or #548 will fix this issue!