-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Accessing attributes in modules imported by import_module #5059
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
Comments
This is a duplicate of #1424, we don't currently support |
Thank you for the reply. This issue is not about |
Importing something based on an environment variable is too dynamic, mypy cannot statically know what module you are reffering to.
Perhaps we should warn when someone calls |
Right, I get it, thanks for the explanation. Sometimes it's unavoidable to use |
I think that the return type of |
Or perhaps types.ModuleType should grow a |
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:
Which can only be fixed by using |
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>
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
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
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
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
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
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
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
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
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
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
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
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
The text was updated successfully, but these errors were encountered: