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

Accessing attributes in modules imported by import_module #5059

Closed
pawelswiecki opened this issue May 16, 2018 · 7 comments
Closed

Accessing attributes in modules imported by import_module #5059

pawelswiecki opened this issue May 16, 2018 · 7 comments

Comments

@pawelswiecki
Copy link

pawelswiecki commented May 16, 2018

Are you reporting a bug, or opening a feature request?
I believe I found a bug in mypy (as far as I understand what's going on, this is not a typeshed issue).

The code

# my_file.py
from importlib import import_module
import os

settings_module_path = os.getenv('PRODUCTION_SETTINGS_MODULE')
settings = import_module(settings_module_path)

if settings and hasattr(settings, 'LOGGING'):
    log_config = settings.LOGGING

Behaviour

$ mypy 
my_file.py:8: error: Module has no attribute "LOGGING"

Expected behavior
No mypy error. Especially, when the attribute is explicitly asserted to be there (analogically as with narrowing types using isinstance()).

Versions
Python 3.6.5
mypy==0.600

@ethanhs
Copy link
Collaborator

ethanhs commented May 16, 2018

This is a duplicate of #1424, we don't currently support hasattr checks.

@ethanhs ethanhs closed this as completed May 16, 2018
@pawelswiecki
Copy link
Author

pawelswiecki commented May 16, 2018

Thank you for the reply.

This issue is not about hasattr checks, though. (It was just a suggestion how it could be dealt with.) It's about modules imported using import_module. Shouldn't mypy approach differently to them? Adding # type: ignore seems the only option I have, which I'd love to avoid.

@ethanhs
Copy link
Collaborator

ethanhs commented May 16, 2018

Importing something based on an environment variable is too dynamic, mypy cannot statically know what module you are reffering to.

importlib.import_module returns type types.ModuleType. We don't know anything else about the attributes of that module, beyond the attributes of that type. Therefore, the error message boils down to #1424.

Perhaps we should warn when someone calls __import__, importlib.import_module or the like, because it almost always requires a type ignore.

@pawelswiecki
Copy link
Author

pawelswiecki commented May 16, 2018

Right, I get it, thanks for the explanation. Sometimes it's unavoidable to use import_module (as in my example), warning won't help in my opinion, since the user is probably aware of the situation. I will wait for resolving #1424. Thanks again.

@JukkaL
Copy link
Collaborator

JukkaL commented May 16, 2018

I think that the return type of importlib.import_module should be Any, similar to how the type of getattr is Any.

@JelleZijlstra
Copy link
Member

Or perhaps types.ModuleType should grow a def __getattr__(self, attr) -> Any, because modules can have arbitrary attributes.

@patrick91
Copy link
Contributor

I'm having problem with this too:

@strawberry.type
class TypeB:
    @strawberry.field(
        type=lambda: importlib.import_module(".type_a", __package__).TypeA
    )
    def type_a(self, info) -> "TypeA":
        from .type_a import TypeA

        return TypeA()

I get this error:

Module has no attribute "TypeA"mypy(error)

Which can only be fixed by using # type: ignore

fengguang pushed a commit to 0day-ci/linux that referenced this issue Oct 22, 2021
Currently, we have these errors:
$ mypy ./tools/testing/kunit/*.py
tools/testing/kunit/kunit_kernel.py:213: error: Item "_Loader" of "Optional[_Loader]" has no attribute "exec_module"
tools/testing/kunit/kunit_kernel.py:213: error: Item "None" of "Optional[_Loader]" has no attribute "exec_module"
tools/testing/kunit/kunit_kernel.py:214: error: Module has no attribute "QEMU_ARCH"
tools/testing/kunit/kunit_kernel.py:215: error: Module has no attribute "QEMU_ARCH"

exec_module
===========

pytype currently reports no errors, but that's because there's a comment
disabling it on 213.

This is due to python/typeshed#2626.
The fix is to assert the loaded module implements the ABC
(abstract base class) we want which has exec_module support.

QEMU_ARCH
=========

pytype is fine with this, but mypy is not:
python/mypy#5059

Add a check that the loaded module does indeed have QEMU_ARCH.
Note: this is not enough to appease mypy, so we also add a comment to
squash the warning.

Signed-off-by: Daniel Latypov <dlatypov@google.com>
elParaguayo added a commit to elParaguayo/qtile that referenced this issue Oct 24, 2021
Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: qtile#2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
elParaguayo added a commit to elParaguayo/qtile that referenced this issue Oct 24, 2021
Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: qtile#2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
- various wayland files: missing imports, adding assertions to help
  identity optional type, correcting types
elParaguayo added a commit to elParaguayo/qtile that referenced this issue Oct 26, 2021
Removed mypy from testenv
- No benefit of running mypy tests in every environment
- py39 has `mypy` environment which runs `test_check` and `test_migrate`
  so tests will be run.

Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: qtile#2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
- various wayland files: missing imports, adding assertions to help
  identity optional type, correcting types
elParaguayo added a commit to elParaguayo/qtile that referenced this issue Oct 27, 2021
Removed mypy from testenv
- No benefit of running mypy tests in every environment
- py39 has `mypy` environment which runs `test_check` and `test_migrate`
  so tests will be run.

Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: qtile#2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
- various wayland files: missing imports, adding assertions to help
  identity optional type, correcting types
elParaguayo added a commit to elParaguayo/qtile that referenced this issue Oct 30, 2021
Removed mypy from testenv
- No benefit of running mypy tests in every environment
- py39 has `mypy` environment which runs `test_check` and `test_migrate`
  so tests will be run.

Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: qtile#2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
- various wayland files: missing imports, adding assertions to help
  identity optional type, correcting types
elParaguayo added a commit to elParaguayo/qtile that referenced this issue Oct 31, 2021
Removed mypy from testenv
- No benefit of running mypy tests in every environment
- py39 has `mypy` environment which runs `test_check` and `test_migrate`
  so tests will be run.

Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: qtile#2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
- various wayland files: missing imports, adding assertions to help
  identity optional type, correcting types
elParaguayo added a commit to elParaguayo/qtile that referenced this issue Oct 31, 2021
Removed mypy from testenv
- No benefit of running mypy tests in every environment
- py39 has `mypy` environment which runs `test_check` and `test_migrate`
  so tests will be run.

Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: qtile#2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
- various wayland files: missing imports, adding assertions to help
  identity optional type, correcting types
elParaguayo added a commit to qtile/qtile that referenced this issue Nov 1, 2021
Removed mypy from testenv
- No benefit of running mypy tests in every environment
- py39 has `mypy` environment which runs `test_check` and `test_migrate`
  so tests will be run.

Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: #2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
- various wayland files: missing imports, adding assertions to help
  identity optional type, correcting types
fdev31 pushed a commit to fdev31/qtile that referenced this issue May 30, 2022
Removed mypy from testenv
- No benefit of running mypy tests in every environment
- py39 has `mypy` environment which runs `test_check` and `test_migrate`
  so tests will be run.

Added missing dependencies for tox mypy tests:
- dbus-next
- PyGObject (needed for dbus-next's glib interface which we don't
  use...)

Fixed failures in codebase:
- sh.py: qtile#2936 fixed a bug but moved an import into an `import_module`
  call. As per python/mypy#5059 mypy can't
  follow this so we need to add `# type: ignore` to the impacted lines.
- run_cmd.py: duplicate variable name fixed.
- battery.py: `background` and `low_background` need types because of
  additional logic in widget.
- scripts/main.py: silence mypy on `distribution` import as will fail
  for python 3.7
- various wayland files: missing imports, adding assertions to help
  identity optional type, correcting types
polm added a commit to polm/spaCy that referenced this issue Sep 2, 2022
Mypy throws an error for any reference to a module member that doesn't
exist, even with a clear hasattr check like in this code.

python/mypy#5059
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

5 participants