-
Notifications
You must be signed in to change notification settings - Fork 131
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
Settle on Best Practice for Union / Optional Type Annotations #619
Comments
My preference is to use class MyClass:
def me(self) -> "MyClass | None":
return self as opposed to: from typing import Optional
class MyClass:
def me(self) -> Optional["MyClass"]:
return self Where, obviously, Reasons:
|
I'm convinced by @achidlow's argument. Anyone else care to opine? @michaeldiamant @jasonpaulos @ahangsu |
I have no objections |
either way's fine with me |
I'll follow the majority opinion. I'm less concerned here because the decision feels like it can be changed with minimal friction should a compelling alternative arise later. |
Problem
PEP 604 allows the pipe symbol (
|
) to annotate unions of types. In particular:X | Y
Union[X,Y]
and
X | None
Optional[X]
However, type forwarding does not work with this new capability. I.e.
"Xtype" | Y
doesn't work (though"Xtype | Y"
does actually work).Currently our repo is inconsistent when it comes to union types.
Solution
We should settle on a best practice for such annotations, change all usages to adhere to the best practice, and add it to our style guide.
I propose that we disallow
|
because of its incompatibility with forwarded types. But I'm open to discussion and don't have a strong opinion, except that we should have some convention.Dependencies
None
Urgency
Very low
The text was updated successfully, but these errors were encountered: