8000 False negative when using magic methods · Issue #8027 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

False negative when using magic methods #8027

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
fsouza opened this issue Nov 27, 2019 · 4 comments
Closed

False negative when using magic methods #8027

fsouza opened this issue Nov 27, 2019 · 4 comments

Comments

@fsouza
Copy link
fsouza commented Nov 27, 2019

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?

Bug?

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import List
from typing import Optional


class CustomClass:
    def __getitem__(self, key: str) -> Optional[str]:
        pass


items: List[CustomClass] = []
[item["a"].strip() for item in items if item["a"] is not None]

The one below doesn't type check, as expected:

from typing import List
from typing import Optional


class CustomClass:
    def getitem(self, key: str) -> Optional[str]:
        pass


items: List[CustomClass] = []
[item.getitem("a").strip() for item in items if item.getitem("a") is not None]
  • What is the actual behavior/output?
Success: no issues found in 1 source file
  • What is the behavior/output you expect?

It should not type check, as repeated calls to __getitem__ are not guaranteed to return the same value.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?
% mypy --version
mypy 0.750+dev.1013b63eac2bef395952f2784fb3390dc181dec7
% python --version
Python 3.7.5
  • What are the mypy flags you are using? (For example --strict-optional)

--strict --scripts-are-modules

@fsouza fsouza changed the title False positive when using magic methods False negative when using magic methods Nov 27, 2019
@ilevkivskyi
Copy link
Member

This is a duplicate of #7339

@fsouza
Copy link
Author
fsouza commented Nov 27, 2019

@ilevkivskyi doesn't #7339 cover a false positive? This here is a false negative 🤔 Or is that issue supposed to cover everything-indexing?

Type refinement can't be safely accomplished in the sample code, therefore mypy should reject it.

@ilevkivskyi
Copy link
Member

OK, I see. Then this is a duplicate of a different issue #4168 (modulo attribute -> index). The latter has also false-positive label because overly narrow types cause troubles with --strict-equality

@fsouza
Copy link
Author
fsouza commented Nov 28, 2019

Thanks for the info!

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
0