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

Stdlib: add 'obvious' default values #9688

Merged
merged 1 commit into from
Feb 7, 2023

Conversation

AlexWaygood
Copy link
Member

All parameters annotated with Literal[False] must, logically speaking, have a default of False (if they have a default value at all). The same goes for parameters annotated with None, Literal[True], Literal[0], Literal["foo"], etc. etc.

I ran this code in the interactive REPL to create this PR:

>>> from pathlib import Path
>>> import re
>>> for path in Path("stdlib").rglob("*.pyi"):
...     source = path.read_text()
...     new_source = re.sub(r": Literal\[([a-zA-Z0-9_\"]+)\] = \.\.\.", r": Literal[\1] = \1", source)
...     new_source = re.sub(r": None = \.\.\.", ": None = None", new_source)
...     if new_source != source:
...         path.write_text(new_source)

@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2023

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Didn't check against the implementation, but the reasoning (and spot checking) is sound.

@srittau srittau merged commit 53747b2 into python:main Feb 7, 2023
@AlexWaygood AlexWaygood deleted the obvious-defaults branch February 7, 2023 12:00
@AlexWaygood
Copy link
Member Author

I ran this code in the interactive REPL to create this PR:

>>> from pathlib import Path
>>> import re
>>> for path in Path("stdlib").rglob("*.pyi"):
...     source = path.read_text()
...     new_source = re.sub(r": Literal\[([a-zA-Z0-9_\"]+)\] = \.\.\.", r": Literal[\1] = \1", source)
...     new_source = re.sub(r": None = \.\.\.", ": None = None", new_source)
...     if new_source != source:
...         path.write_text(new_source)

@JelleZijlstra we could possibly add this kind of logic to stubdefaulter, but I'm not sure if it would be a "good fit" with the rest of the code. WDYT?

@JelleZijlstra
Copy link
Member

I'd be open to adding it.

@AlexWaygood
Copy link
Member Author

I'd be open to adding it.

JelleZijlstra/stubdefaulter#54 :)

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