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

Getting "error: Module has no attribute" when a 'try' block catching AttributeError #8165

Closed
brettcannon opened this issue Dec 17, 2019 · 2 comments

Comments

@brettcannon
Copy link
Member

  • 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.
        try:
            # os.confstr("CS_GNU_LIBC_VERSION") returns a string like "glibc 2.17".
            version_string = os.confstr("CS_GNU_LIBC_VERSION")
            assert version_string is not None
            _, version = version_string.split()
        except (AssertionError, AttributeError, OSError, ValueError):
            # os.confstr() or CS_GNU_LIBC_VERSION not available (or a bad value)...
            return None
        return version
  • What is the actual behavior/output? "error: Module has no attribute "confstr"
  • What is the behavior/output you expect? No warning as the try block captures AttributeError
  • What are the versions of mypy and Python you are using? mypy 0.750, Python 3.7.4
  • What are the mypy flags you are using? (For example --strict-optional) None specifically (using https://github.com/pre-commit/mirrors-mypy)
  • If mypy crashed with a traceback, please paste
    the full traceback below.

(You can freely edit this text, please remove all the lines
you believe are unnecessary.)

@ilevkivskyi
Copy link
Member

From mypy point of view this is the same as:

try:
    42 + "no"
except TypeError:
    pass

Putting this to even more extreme what if a user has

try:
    main()
except Exception:
    fail_gracefully()

Should mypy just show no errors in the whole program? I think no. There were some discussions about supporting checked exceptions, but even if this will be ever supported, it will be likely only for user-defined exceptions, not for builtin ones.

Realistically this will be never supported, so I would propose to close this unless someone disagrees. For now you can put # type: ignore[attr-defined] there.

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 29, 2020

Yeah, this looks too complicated to support in any generality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants