Skip to content

use explicit Optional[...] for parameters with None as a default in datastructures.py#1448

Merged
Kludex merged 5 commits intoKludex:masterfrom
adriangb:use-explicit-optional
Jan 31, 2022
Merged

use explicit Optional[...] for parameters with None as a default in datastructures.py#1448
Kludex merged 5 commits intoKludex:masterfrom
adriangb:use-explicit-optional

Conversation

@adriangb
Copy link
Contributor

@adriangb adriangb commented Jan 29, 2022

@adriangb adriangb changed the title use explicit Optional[...] for paramters with None as a default use explicit Optional[...] for parameters with None as a default in datastructures.py Jan 29, 2022
@Kludex
Copy link
Owner

Kludex commented Jan 30, 2022

See https://www.python.org/dev/peps/pep-0484/#id29

The link is "not found".

@adriangb
Copy link
Contributor Author

adriangb commented Jan 30, 2022

Weird, it got mangled, it should be https://www.python.org/dev/peps/pep-0484/#union-types

@adriangb
Copy link
Contributor Author

Specifically:

A past version of this PEP allowed type checkers to assume an optional type when the default value is None, as in this code:

def handle_employee(e: Employee = None): ...

This would have been treated as equivalent to:

def handle_employee(e: Optional[Employee] = None) -> None: ...

This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit.

Copy link
Contributor

@lovelydinosaur lovelydinosaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Don't know what the motivation is behind...

This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit.

But happy enough either ways. 😌

@Kludex Kludex merged commit d6269e2 into Kludex:master Jan 31, 2022
@adriangb adriangb deleted the use-explicit-optional branch January 31, 2022 15:21
@adriangb
Copy link
Contributor Author

There's probably discussions on the topic in BPO and such. Personally my motivation is just to make sure the project works as well as possible with type checkers.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants