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

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

Closed
7 tasks done
wizard-28 opened this issue Mar 17, 2022 · 11 comments · Fixed by #548
Closed
7 tasks done

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

wizard-28 opened this issue Mar 17, 2022 · 11 comments · Fixed by #548
Labels
bug Something isn't working

Comments

@wizard-28
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • 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 wizard-28 added the question Question or problem label Mar 17, 2022
wizard-28 added a commit to pacstall/pacup.py that referenced this issue Mar 17, 2022
There's a bug with `typer` when using `annotations` from `__future__`.
So revert to using vanilla python type annotations.

BUG: fastapi/typer#371
@ankitpv
Copy link

ankitpv commented Mar 21, 2022

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

@wizard-28
Copy link
Author

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

@astrochun
Copy link

I'm getting the same issue with python 3.10:

RuntimeError: Type not yet supported: str | None

@jrmidkiff
Copy link

jrmidkiff commented Oct 20, 2022

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

@parallaxe
Copy link

parallaxe commented Oct 24, 2022

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?

@johnthagen
Copy link

Duplicate of

FriendlyUser added a commit to FriendlyUser/EdgeGPT that referenced this issue Mar 5, 2023
Update for python3.8, support future annotations, see fastapi/typer#371
sir-gon pushed a commit to sir-gon/algorithm-exercises-py that referenced this issue Jun 21, 2023
@svlandeg
Copy link
Member

svlandeg commented Apr 11, 2024

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

@Asthestarsfalll
Copy link
Contributor

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

Hi, When will those PRs merge?

@svlandeg svlandeg linked a pull request May 21, 2024 that will close this issue
@svlandeg
Copy link
Member

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

Hi, When will those PRs merge?

When Tiangolo has a chance to review them - he's aware of them, but has many other things to do as well 😉

@alanwilter
Copy link

alanwilter commented Jun 19, 2024

Oh, I need this fix please! I started refactoring to use typer but I now I'm on hold again.

@blackpearl1022
Copy link

def to_numpy(x: torch.Tensor | np.ndarray | dict | list) -> np.ndarray: TypeError: unsupported operand type(s) for |: 'torch._C._TensorMeta' and 'type'

How to do in this case ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment