-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Support configparser.UNNAMED_SECTION (#13542) #13544
Conversation
UNNAMED_SECTION was added in Python 3.13 and can be used in place of any section name. It is not actually a string though, so it needs a separate type.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I don't think, the DEFAULT section can be used everywhere. See below.
@srittau Not sure what you're talking about; this PR is about unnamed section, which is unrelated to the default section and can absolutely be used for sections() and options(). |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was mixing up unnamed and default sections it seems. That said, can you try one thing, please. See below. This will hopefully fix the new mypy primer hits.
stdlib/configparser.pyi
Outdated
def has_section(self, section: str) -> bool: ... | ||
def options(self, section: str) -> list[str]: ... | ||
def has_option(self, section: str, option: str) -> bool: ... | ||
def sections(self) -> list[_SectionName]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def sections(self) -> list[_SectionName]: ... | |
# This list can only include an unnamed section if the parser was initialized with | |
# allow_unnamed_section=True. Any prevents users from having to use explicit | |
# type checks if allow_unnamed_section is False (the default). | |
def sections(self) -> list[str | _UNNAMED_SECTION | Any]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will not work because _UNNAMED_SECTION
does not exist for python < 3.13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of this in general because it limits the usefulness of type checking, but if it's a requirement that mypy_primer does not fail we can try it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably better to have separate branches for Python >= 3.13 and Python < 3.12 here anyway, so that < 3.12 can get tighter types here.
In typeshed, usability is a major concern. As the primer output shows, code that is currently working correctly would need additional churn with this change. This is something we want to avoid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you mean "< 3.13". It already has tighter types because of the type aliases. <3.13 will get list[str]
, as before.
As the types were objectively wrong before (as far as >= 3.13 is concerned!), it will be difficult to get it working without changes to existing code. What do you suggest?
I was mixing up unnamed and default sections.
This comment has been minimized.
This comment has been minimized.
@srittau It does not help, unfortunately. |
Hmm, yes I wasn't sure if the "Any trick" worked in this situation. Unfortunately, the best we can do here is use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
UNNAMED_SECTION was added in Python 3.13 and can be used in place of any section name. It is not actually a string though, so it needs a separate type.