Skip to content

dict incorrectly being detected as "object" #1786

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
noisecapella opened this issue Jul 1, 2016 · 4 comments
Closed

dict incorrectly being detected as "object" #1786

noisecapella opened this issue Jul 1, 2016 · 4 comments

Comments

@noisecapella
Copy link

I narrowed down the problem to this repro case:

LOGGING = {
    'handlers': {
    },
    'loggers': {
        'django': {
        },
        'django.request': {
        },
        'urllib3': {
        }
    },
}

LOGGING['handlers'].pop('syslog')

When I run mypy 0.4.2 on this file (named test.py) I get this error:

test.py:14: error: "object" has no attribute "pop"

Thanks for your help!

@gvanrossum
Copy link
Member

I'd add a type annotation to the LOGGING variable, something like # type: Dict[str, <whatever you want>].

Do note that mypy currently doesn't support different (fixed) keys of a dict having different types -- that's probably what's biting you. That and the empty dicts.

@noisecapella
Copy link
Author

The type annotation addresses the warning I got. FYI I got another warning after adding the type annotation. Not sure if it's a known problem or not but adding it here in case it's helpful. Here is the updated input:

LOGGING = {
    'handlers': {
    },
    'loggers': {
        'django': {
        },
        'django.request': {
        },
        'urllib3': {
        }
    },
   }  # type: Dict[str, Dict[str, str]]

LOGGING['handlers'].pop('syslog')

Warning:

test.py:4: error: List item 0 has incompatible type "Tuple[str, Dict[None, None]]"
test.py:4: error: List item 1 has incompatible type "Tuple[str, Dict[None, None]]"
test.py:4: error: List item 2 has incompatible type "Tuple[str, Dict[None, None]]"

@rwbarton
Copy link
Contributor

rwbarton commented Jul 1, 2016

It looks like your LOGGING variable actually has a type like Dict[str, Dict[str, Dict[_, _]]] for some _s.

@noisecapella
Copy link
Author

Yes you're right. When I update the type annotation accordingly it fixes the problem

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

3 participants