-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Use more precise type for gettext.find
#6980
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
It defaults to |
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
stdlib/gettext.pyi
Outdated
domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: Literal[True] = ... | ||
) -> list[str]: ... | ||
@overload | ||
def find( | ||
domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: Literal[False] = ... | ||
) -> str | None: ... | ||
@overload | ||
def find(domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: bool = ...) -> 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.
domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: Literal[True] = ... | |
) -> list[str]: ... | |
@overload | |
def find( | |
domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: Literal[False] = ... | |
) -> str | None: ... | |
@overload | |
def find(domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: bool = ...) -> Any: ... | |
domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: Literal[True] | |
) -> list[str]: ... | |
@overload | |
def find( | |
domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: Literal[False] = ... | |
) -> str | None: ... | |
@overload | |
def find(domain: str, localedir: StrPath | None = ..., languages: Iterable[str] | None = ..., all: bool) -> Any: ... |
I think this should get rid of the "overlapping overloads" complaint!
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 think we can't do that. Because in your example all
without a default follows args with defaults. It is a syntax error.
I've tested this piece to work correctly with a single ignore:
from typing import Any, overload, Literal
@overload # ignores incompatible overloads
def find( # type: ignore[misc]
domain: str, all: Literal[False] = ...
) -> str | None: ...
@overload
def find(
domain: str, all: Literal[True] = ...
) -> list[str]: ...
@overload
def find(
domain: str, all: bool = ...
) -> Any: ...
reveal_type(find('a')) # N: Revealed type is "Union[builtins.str, None]"
reveal_type(find('a', all=True)) # N: Revealed type is "builtins.list[builtins.str]"
reveal_type(find('a', all=False)) # N: Revealed type is "Union[builtins.str, None]"
reveal_type(find('a', all=1 in {})) # N: Revealed type is "Any"
Btw, Any
here still looks a bit to wide to me 🤔
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.
Oh shoot, I see.
Btw, Any here still looks a bit to wide to me 🤔
Maybe str | None | Any
? (Or something similarly 🌟fancy🌟?)
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.
Let's keep Any
, if anyone else needs a narrower type, we can always add it later 👍
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.
The usual solution is to split each non-= ...
overload into two cases: one that has the argument (and everything before it) specified positionally, and another where it is specified as a keyword argument. This would be a total of 5 overloads, but it would prevent overlapping overloads. I'm fine with it either way, but if you think that would be cleaner, do that instead :)
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 think that 3
with a type: ignore
comment (current) is cleaner 👍
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Source: https://github.com/python/cpython/blob/b04dfbbe4bd7071d46c8688c2263726ea31d33cd/Lib/gettext.py#L467-L500