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

Use more precise type for gettext.find #6980

Merged
merged 7 commits into from
Jan 20, 2022
Merged

Use more precise type for gettext.find #6980

merged 7 commits into from
Jan 20, 2022

Conversation

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 20, 2022

It defaults to False, so we probably only need a default value for the all parameter in the Literal[False] overload, right? If the user doesn't supply a value for that parameter, we can force mypy to choose the Literal[False] overload.

stdlib/gettext.pyi Outdated Show resolved Hide resolved
sobolevn and others added 2 commits January 20, 2022 18:03
Comment on lines 32 to 39
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: ...
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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!

Copy link
Member Author

@sobolevn sobolevn Jan 20, 2022

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 🤔

Copy link
Member

@AlexWaygood AlexWaygood Jan 20, 2022

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🌟?)

Copy link
Member Author

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 👍

Copy link
Collaborator

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 :)

Copy link
Member Author

@sobolevn sobolevn Jan 20, 2022

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 👍

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

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

@Akuli Akuli merged commit 7dd3555 into python:master Jan 20, 2022
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