Skip to content
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

Error when copying list from dict with generic key #1995

Closed
jo-sm opened this issue Aug 7, 2016 · 7 comments
Closed

Error when copying list from dict with generic key #1995

jo-sm opened this issue Aug 7, 2016 · 7 comments

Comments

@jo-sm
Copy link

jo-sm commented Aug 7, 2016

I have a dict inside of a function that's laid out like this:

def foo(type: str):
  a = {
    'js': {
      'tag': 'script',
      'url_attr': 'src',
      'attrs': [
        ('type', 'text/javascript')
      ],
      'close': True
    },
    'css': {
      'tag': 'link',
      'url_attr': 'href',
      'attrs': [
        ('rel', 'stylesheet'),
        ('type', 'text/css')
      ]
    }
  }

  # error: `No overload variant of "list" matches argument types [builtins.object*]`
  list_with_tuples = list(a[type]['attrs']) # Copying via list(...)

  # error: "object" has no attribute "copy"
  list_with_tuples = a[type]['attrs'].copy() # Copying via list.copy()

When I run the code above through mypy, I get the errors listed above. Does mypy have issues seeing that attrs in a[type] will resolve to a list? Or is there a way to get around this error message?

@gvanrossum
Copy link
Member

What version of mypy and typeshed are you using? I cannot reproduce this. :-(

@jo-sm
Copy link
Author

jo-sm commented Aug 8, 2016

How do I see which version of typeshed do I have? Maybe that's it...

In any case, I'm using 0.4.3 version of mypy:

> mypy -V
mypy 0.4.3

@gvanrossum
Copy link
Member

I'm sorry, I still can't reproduce this, even with 0.4.3. Can you try uninstalling mypy and reinstalling (preferably from the repo)?

FWIW if you pip install mypy, the correct version of typeshed should automatically be installed.

Do you have MYPYPATH set?

Are you sure the code you're testing is exactly what you posted here?

@jo-sm
Copy link
Author

jo-sm commented Aug 8, 2016

Ah you know what, now I'm more confused... I made a dict that I thought was effectively the same, but it apparently is not. I updated the issue with the exact dict I'm using in my code.

@gvanrossum
Copy link
Member

Aha, now I understand your issue. Mypy doesn't (yet) understand that different keys can have values of different types. So it understands that a is a dict whose strings are keys and whose values are dicts again, and the latter dicts have string keys, and values that could be:

  • strings, e.g. a['js']['tag']; or
  • lists of (str, str) tuples, e.g. a['js']['attrs'].

There's discussion of a feature that would make your code work here: python/typing#28. But it requires more thought. In the meantime I'm going to close this issue as "won't fix" -- if we decide to add this to PEP 484 (or a follow-up PEP) we'll open a mypy task to implement it.

@jo-sm
Copy link
Author

jo-sm commented Aug 11, 2016

That makes sense. Thanks for looking into it, I really appreciate it!

In the mean time, is there any way to ignore this error, like how pep8 allows for ignoring specific E* rules? Ideally I'd like to add mypy to some report generation along with our test suite, so we can see if there's any typing mistakes during code review.

@rwbarton
Copy link
Contributor

You can ignore errors on specific lines with # type: ignore comments. Some projects have a convention of including a reference to a relevant mypy ticket as explanation, like

  # type: ignore # https://github.com/python/typing/issues/28

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