8000 Specialize empty collections to Any as a fallback · Issue #15433 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Specialize empty collections to Any as a fallback #15433

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

Open
superbobry opened this issue Jun 14, 2023 · 0 comments
Open

Specialize empty collections to Any as a fallback #15433

superbobry opened this issue Jun 14, 2023 · 0 comments
Labels

Comments

@superbobry
Copy link

Context

I have a pending PR for typeshed which fixes overparameterized dict.get and dict.pop methods: python/typeshed#10294. The PR causes a lot of failures in various GitHub projects due to how mypy treats empty collections. Specifically, if dict.get is annotated as

class dict(Generic[KT, VT]):
    def get(self, key: KT, default: T) -> T | VT: ...

mypy infers the type of v in

def test(d: dict[str, Any]):
    v = d.get("foo", {})

to be Any | dict[<unknown>, <unknown>], instead of Any | dict[Any, Any].

Feature

Specialize empty collections to Any if no other information is available to recover type arguments.

Pitch

Using <unknown> instead of Any requires users to cast or add type annotations where they otherwise will not be necessary. See comments in the PR for a few examples from real projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant
0