From 52facccf3d25ab0481bf14f303f908bd333b462a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9amus=20=C3=93=20Ceanainn?= Date: Fri, 25 Oct 2024 23:09:59 +0100 Subject: [PATCH] Make `disallow-any-unimported` flag invertible (#18030) The `--follow-imports=skip` CLI option is pretty much unusable in a project where `disallow_any_unimported` is set to true in the configuration file, as this results in a large number of errors (due to both flags being incompatible). We have a pretty standard project configuration file (with `disallow_any_unimported = true` and `follow_imports = 'normal'`), but for specific local development cases where we want to run the mypy CLI with `--follow-imports=skip` it's incredibly noisy due to the number of errors produced. This change proposes making the `disallow-any-unimported` invertible, so that the CLI can be used with `--follow-imports=skip` in a less noisy way by using: ```bash mypy --follow-imports=skip --allow-any-unimported path/to/my/file.py ``` --- mypy/main.py | 12 ++++++------ test-data/unit/check-flags.test | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index 5991ddd9e62f0..4860b93400c2e 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -645,12 +645,6 @@ def add_invertible_flag( title="Disallow dynamic typing", description="Disallow the use of the dynamic 'Any' type under certain conditions.", ) - disallow_any_group.add_argument( - "--disallow-any-unimported", - default=False, - action="store_true", - help="Disallow Any types resulting from unfollowed imports", - ) disallow_any_group.add_argument( "--disallow-any-expr", default=False, @@ -677,6 +671,12 @@ def add_invertible_flag( help="Disallow usage of generic types that do not specify explicit type parameters", group=disallow_any_group, ) + add_invertible_flag( + "--disallow-any-unimported", + default=False, + help="Disallow Any types resulting from unfollowed imports", + group=disallow_any_group, + ) add_invertible_flag( "--disallow-subclassing-any", default=False, diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index dd7bee3f7aec3..c6419923ebc62 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -963,6 +963,12 @@ from missing import Unchecked t: Unchecked = 12 # E: Type of variable becomes "Any" due to an unfollowed import +[case testAllowImplicitAnyVariableDefinition] +# flags: --ignore-missing-imports --allow-any-unimported +from missing import Unchecked + +t: Unchecked = 12 + [case testDisallowImplicitAnyGeneric] # flags: --ignore-missing-imports --disallow-any-unimported from missing import Unchecked