Skip to content

Type hints for __new__ no longer work when subclassing Dict #15135

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

Closed
ShaneHarvey opened this issue Apr 25, 2023 · 2 comments
Closed

Type hints for __new__ no longer work when subclassing Dict #15135

ShaneHarvey opened this issue Apr 25, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@ShaneHarvey
Copy link

Bug Report

mypy 1.1.1 introduces a new false positive on pymongo's codebase first noticed in python/typeshed#9694 (comment)

Type hints for __new__ no longer work when subclassing Dict (or dict).

To Reproduce

I see the same behavior on 1.2 and latest:

$ cat repro.py 
from typing import Dict, Any, Type, TypeVar

_Key = TypeVar("_Key")
_Value = TypeVar("_Value")


class MyDict(Dict[_Key, _Value]):
    def __new__(cls: "Type[MyDict[_Key, _Value]]", *args: Any, **kwargs: Any) -> "MyDict[_Key, _Value]":
        return super().__new__(cls, *args, **kwargs)

Error:

$ mypy --strict repro.py           
repro.py:9: error: Value of type variable "Self" of "__new__" of "dict" cannot be "MyDict[_Key, _Value]"  [type-var]
Found 1 error in 1 file (checked 1 source file)

Current workaround is to add # type: ignore[type-var].

Your Environment

  • Mypy version used: 1.1.1, 1.2.0, 1.4.0+dev.bd6ce237f62366f156f4af074e398d761fb41359
  • Mypy command-line flags: --strict although I'm not sure it's needed.
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.1

Note that in the real code, __new__ is needed to support pickle.

@finite-state-machine
Copy link

This applies to (at least) FrozenSet as well.

mypy-play.net: minimal reproduction case

As motivation, consider [mypy-play.net]:

from __future__ import annotations
from typing import *

class ModFrozenSet(FrozenSet[int]):

    some_str: str

    def __new__(cls, init: Iterable[int], some_str: str) -> ModFrozenSet:
        _ = some_str  # used by '__init__()'
        return frozenset.__new__(cls, init)

    def __init__(self, init: Iterable[int], some_str: str):
        _ = init  # used by '__new__()'
        self.some_str = some_str


# Empirical test to show that this works in practice:

def test_motivation_empirically() -> None:

    created = ModFrozenSet([1, 2, 3], "hello")

    assert type(created) is ModFrozenSet
    assert isinstance(created, frozenset)
    assert sorted(created) == [1, 2, 3]
    assert created.some_str == "hello"

@finite-state-machine
Copy link

This appears to be fixed on the master branch, but not in 1.6.1 (per the first gist in my reply above).

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

No branches or pull requests

3 participants