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

[EMH] Implement alternative versions of Optional[...] #6254

Open
matusvalo opened this issue Jun 20, 2024 · 1 comment
Open

[EMH] Implement alternative versions of Optional[...] #6254

matusvalo opened this issue Jun 20, 2024 · 1 comment

Comments

@matusvalo
Copy link
Contributor

matusvalo commented Jun 20, 2024

Describe your issue

It seems that implementing generic union types (X | Y and Union[X, Y]) are difficult to implement and are rejected. We can try to implement reduced version containing None since it is often used instead of typing.Optional[]. We can transform it as follows:

  • X | None = None | X -> typing.Optional[X]
  • X | Y | None = X | None | Y = None | X | Y -> typing.Optional[object]
  • typing.Union[X, None] = typing.Union[None, X] -> typing.Optional[X]
  • typing.Union[X, Y, None] = typing.Union[X, None, Y] = typing.Union[None, X, Y] -> typing.Optional[object]

This issue is specialised version of #4631

@matusvalo matusvalo changed the title Implement alternative versions of Optional[...] [EMJImplement alternative versions of Optional[...] Jun 20, 2024
@matusvalo matusvalo changed the title [EMJImplement alternative versions of Optional[...] [EMH] Implement alternative versions of Optional[...] Jun 20, 2024
@da-woods
Copy link
Contributor

X | Y | None = X | None | Y = None | X | Y -> typing.Optional[object]
typing.Union[X, Y, None] = typing.Union[X, None, Y] = typing.Union[None, X, Y] -> typing.Optional[object]

These two aren't hugely worthwhile in Cython (because if you type something as object we never generate code that assumes it isn't None). They're harmless, but covered by the current behaviour of "annotation that Cython doesn't understand -> object"

X | None = None | X -> typing.Optional[X]
typing.Union[X, None] = typing.Union[None, X] -> typing.Optional[X]

These two are useful. Especially the one with | since I think that's what most people would choose to write. I have no idea how easy or hard they are to implement.

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

No branches or pull requests

2 participants