typer icon indicating copy to clipboard operation
typer copied to clipboard

TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

Open wizard-28 opened this issue 2 years ago • 10 comments

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

  1. Run the example code
  2. 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

wizard-28 avatar Mar 17 '22 07:03 wizard-28

| 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")):

ankitpv avatar Mar 21 '22 08:03 ankitpv

| 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)

wizard-28 avatar Mar 21 '22 09:03 wizard-28

I'm getting the same issue with python 3.10:

RuntimeError: Type not yet supported: str | None

astrochun avatar Mar 29 '22 04:03 astrochun

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

jrmidkiff avatar Oct 20 '22 19:10 jrmidkiff

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?

parallaxe avatar Oct 24 '22 12:10 parallaxe

Duplicate of

  • #348

johnthagen avatar Jan 03 '23 13:01 johnthagen

Confirmed that either #676 or #548 will fix this issue!

svlandeg avatar Apr 11 '24 12:04 svlandeg