Skip to content

Fallback overloads for literal argument #5493

Open
@srittau

Description

@srittau

Currently, we use an extra str overload when using literals. For example:

@overload
def open(mode: Literal["r"] = ...) -> int: ...
@overload
def open(mode: Literal["w"]) -> str: ...
@overload
def open(mode: str) -> str | int: ...

What's the use case for this? The only thing I can think of is something like this:

mode: str = ...
open(mode)

But this actually looks like a typing error to me where mode should have been annotated with Literal["r", "w"] anyway. I would suggest starting to remove these defaults, possibly after the next mypy version is released. primer should warn us if this causes lots of new errors (as I would expect for e.g builtins.open()) and we can then decide on a case-by-case basis to leave the default in for now.

Side note: We can't do this for bool yet due to python/mypy#6113.

Metadata

Metadata

Assignees

No one assigned

    Labels

    project: policyOrganization of the typeshed project

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions