-
-
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
Improve typing for BS4 element.Tag's get
and get_attribute_list
.
#12840
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Currently, - `get()` will always have `None` as a possible return type, even if the default is set. - `get()`'s typing is wrong if the default's type is not `str` or `list[str]`. - `get_attribute_list()` will never have `None` in the possible types inside the list it returns, even if the default allows for that. - `get_attribute_list()`'s typing is wrong if the default's type is not `str` or `list[str]`. This change improves the type handling for these methods by using the same idea as `stdlib/builtins.pyi` uses for `dict.get()`. This is what `dict.get()`'s typing looks like: ```python @overload # type: ignore[override] def get(self, key: _KT) -> _VT | None: ... @overload def get(self, key: _KT, default: _VT) -> _VT: ... @overload def get(self, key: _KT, default: _T) -> _VT | _T: ... ``` Since Tag.get takes a `str` key and returns a `str` or `list[str]` if the attribute exists, we can do something like a `dict[str, str | list[str]]`: ```python @overload # type: ignore[override] def get(self, key: str) -> str | list | None: ... @overload def get(self, key: str, default: str | list) -> str | list: ... @overload def get(self, key: str, default: _T) -> str | list | _T: ... ``` Then because `str | list == str | list | str | list`, we can simplify: ```python @overload # type: ignore[override] def get(self, key: str) -> str | list | None: ... @overload def get(self, key: str, default: _T) -> str | list | _T: ... ```` We can also do something similar for `get_attribute_list()`: ```python @overload def get_attribute_list(self, key: str) -> list[str | None]: ... @overload def get_attribute_list(self, key: str, default: _T) -> list[str | _T]: ... ``` Except this isn't quite right -- if default is a list, the implementation returns it instead of a list[list], so we need to unwrap it: ```python @overload def get_attribute_list(self, key: str) -> list[str | None]: ... @overload def get_attribute_list(self, key: str, default: list[_T]) -> list[str | _T]: ... @overload def get_attribute_list(self, key: str, default: _T) -> list[str | _T]: ... ```
If you want, I could also change it so we still only allow |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
srittau
requested changes
Oct 18, 2024
Co-authored-by: Sebastian Rittau <[email protected]>
Thanks for the suggestions @srittau -- I should've seen that I could combine those. |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
srittau
approved these changes
Oct 19, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently,
get()
will always haveNone
as a possible return type, even if the default is set.get_attribute_list()
will never haveNone
in the possible types inside the list it returns, even if the default allows for that.get()
andget_attribute_list()
's typing doesn't allow for default to have a type that isn'tstr
orlist[str]
, even though that's technically allowed by the implementation.For reference, here's what the implementation of
get
andget_attribute_list
looks like:This change improves the type handling for these methods by using the same idea as
stdlib/builtins.pyi
uses fordict.get()
.This is what
dict.get()
's typing looks like:Since Tag.get takes a
str
key and returns astr
orlist[str]
if the attribute exists, we have types similar to adict[str, str | list[str]]
:Then because
str | list == str | list | str | list
, we don't actually need the second overload:We can also do something similar for
get_attribute_list()
:Except this isn't quite right -- if the key isn't found and the default is a
list[_T]
, the implementation returns it instead of alist[list[_T]]
, so we need to unwrap it:get()
method_typeshed.MaybeNone
would incorrectly hide cases where the user really should be checking for None.Here's an example of the types that mypy infers for a few cases before and after this change: