Skip to content

[question] proper type annotation for __dict__ #6523

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
AMDmi3 opened this issue Mar 7, 2019 · 2 comments
Closed

[question] proper type annotation for __dict__ #6523

AMDmi3 opened this issue Mar 7, 2019 · 2 comments

Comments

@AMDmi3
Copy link

AMDmi3 commented Mar 7, 2019

I'm wondering which is the correct way of annotating __dict__ property. Dict[str, Any] seem quite obvious and is used multiple times in typeshed, but:

from typing import Any, Dict

class Test:
    @property
    def __dict__(self) -> Dict[str, Any]:
        return {}
% mypy test.py 
test.py:5: error: Signature of "__dict__" incompatible with supertype "object"
@emmatyping
Copy link
Member

emmatyping commented Mar 7, 2019

This appears to be a duplicate of #5836. For example see this minimal repro:

class Test:
    a = ""  # type: str
    
class B(Test):
    @property
    def a(self) -> str:
        return ""

As a workaround in your case, you can likely do something like:

from typing import Dict, Any, TYPE_CHECKING

class Test:
    if TYPE_CHECKING:
        __dict__ = {}  # type: Dict[str, Any]
    else:
        @property
        def __dict__(self) -> Dict[str, Any]:
            return {}

and mypy should be happy.

@ghost
Copy link

ghost commented Apr 12, 2021

#5836 was closed, but this still does not seem to be fixed in mypy 0.812 (Python 3.9.4). Am I missing something here?

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

No branches or pull requests

2 participants