Skip to content

Commit 20135b4

Browse files
ilinumddfisher
authored andcommitted
Add --disallow-any=unannotated as an alias for --disallow-untyped-defs (#3513)
1 parent fd0a416 commit 20135b4

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed

docs/source/command_line.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,22 @@ Here are some more useful flags:
274274
re-check your code without ``--strict-optional`` to ensure new type errors
275275
are not introduced.
276276

277+
.. _disallow-any:
278+
279+
- ``--disallow-any`` disallows various types of ``Any`` in a module.
280+
The option takes a comma-separated list of the following values:
281+
``unimported``, ``unannotated``.
282+
283+
``unimported`` disallows usage of types that come from unfollowed imports
284+
(such types become aliases for ``Any``). Unfollowed imports occur either
285+
when the imported module does not exist or when ``--follow-imports=skip``
286+
is set.
287+
288+
``unannotated`` disallows function definitions that are not fully
289+
typed (i.e. that are missing an explicit type annotation for any
290+
of the parameters or the return type). ``unannotated`` option is
291+
interchangeable with ``--disallow-untyped-defs``.
292+
277293
- ``--disallow-untyped-defs`` reports an error whenever it encounters
278294
a function definition without type annotations.
279295

docs/source/config_file.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ overridden by the pattern sections matching the module name.
147147
- ``almost_silent`` (Boolean, deprecated) equivalent to
148148
``follow_imports=skip``.
149149

150+
- ``disallow_any`` (Comma-separated list, default empty) is an option to
151+
disallow various types of ``Any`` in a module. The flag takes a
152+
comma-separated list of the following arguments: ``unimported``,
153+
``unannotated``. For explanations see the discussion for the
154+
:ref:`--disallow-any <disallow-any>` option.
155+
150156
- ``disallow_untyped_calls`` (Boolean, default False) disallows
151157
calling functions without type annotations from functions with type
152158
annotations.

mypy/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def type_check_only(sources: List[BuildSource], bin_dir: str, options: Options)
9797
options=options)
9898

9999

100-
disallow_any_options = ['unimported']
100+
disallow_any_options = ['unimported', 'unannotated']
101101

102102

103103
def disallow_any_argument_type(raw_options: str) -> List[str]:
@@ -201,7 +201,6 @@ def process_options(args: List[str],
201201

202202
strict_flag_names = [] # type: List[str]
203203
strict_flag_assignments = [] # type: List[Tuple[str, bool]]
204-
disallow_any_options = ['unimported']
205204

206205
def add_invertible_flag(flag: str,
207206
*,
@@ -428,6 +427,9 @@ def add_invertible_flag(flag: str,
428427
print("Warning: --no-fast-parser no longer has any effect. The fast parser "
429428
"is now mypy's default and only parser.")
430429

430+
if 'unannotated' in options.disallow_any:
431+
options.disallow_untyped_defs = True
432+
431433
# Check for invalid argument combinations.
432434
if require_targets:
433435
code_methods = sum(bool(c) for c in [special_opts.modules,
@@ -725,6 +727,8 @@ def parse_section(prefix: str, template: Options,
725727
except ValueError as err:
726728
print("%s: %s: %s" % (prefix, key, err), file=sys.stderr)
727729
continue
730+
if key == 'disallow_any':
731+
results['disallow_untyped_defs'] = v and 'unannotated' in v
728732
if key == 'silent_imports':
729733
print("%s: silent_imports has been replaced by "
730734
"ignore_missing_imports=True; follow_imports=skip" % prefix, file=sys.stderr)

test-data/unit/check-flags.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def f():
4444
1 + "str"
4545
[out]
4646
main:2: error: Function is missing a type annotation
47+
[case testUntypedDefDisallowUnannotated]
48+
# flags: --disallow-any=unannotated
49+
def f():
50+
1 + "str"
51+
[out]
52+
main:2: error: Function is missing a type annotation
4753

4854
[case testSubclassingAny]
4955
# flags: --disallow-subclassing-any

test-data/unit/cmdline.test

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,76 @@ z.py:1: error: Function is missing a type annotation
176176
z.py:4: error: Call to untyped function "f" in typed context
177177
x.py:1: error: Function is missing a type annotation
178178

179+
[case testPerFileConfigSectionUntypedWithDisallowUnannotated]
180+
# cmd: mypy w.py x.py y.py z.py
181+
[file mypy.ini]
182+
[[mypy]
183+
disallow_any = unannotated
184+
[[mypy-y*]
185+
disallow_any =
186+
[[mypy-z*]
187+
disallow_untyped_defs = True
188+
[[mypy-w*]
189+
disallow_untyped_defs = False
190+
[file x.py]
191+
def f(a):
192+
pass
193+
[file y.py]
194+
def f(a):
195+
pass
196+
[file z.py]
197+
def f(a):
198+
pass
199+
[file w.py]
200+
def f(a):
201+
pass
202+
[out]
203+
z.py:1: error: Function is missing a type annotation
204+
x.py:1: error: Function is missing a type annotation
205+
206+
[case testPerFileConfigSectionDisallowUnannotatedWithUntyped]
207+
# cmd: mypy x.py y.py z.py
208+
[file mypy.ini]
209+
[[mypy]
210+
disallow_untyped_defs = True
211+
[[mypy-y*]
212+
disallow_any =
213+
[[mypy-z*]
214+
disallow_any = unannotated
215+
[file x.py]
216+
def f(a):
217+
pass
218+
[file y.py]
219+
def f(a):
220+
pass
221+
[file z.py]
222+
def f(a):
223+
pass
224+
[out]
225+
z.py:1: error: Function is missing a type annotation
226+
x.py:1: error: Function is missing a type annotation
227+
228+
[case testPerFileConfigSectionDisallowUnannotatedNoOverride]
229+
# cmd: mypy x.py y.py z.py
230+
[file mypy.ini]
231+
[[mypy]
232+
[[mypy-x*]
233+
disallow_untyped_defs = True
234+
[[mypy-z*]
235+
disallow_any = unannotated
236+
[file x.py]
237+
def f(a):
238+
pass
239+
[file y.py]
240+
def f(a):
241+
pass
242+
[file z.py]
243+
def f(a):
244+
pass
245+
[out]
246+
z.py:1: error: Function is missing a type annotation
247+
x.py:1: error: Function is missing a type annotation
248+
179249
[case testPerFileConfigSectionMultipleMatches]
180250
# cmd: mypy xx.py xy.py yx.py yy.py
181251
[file mypy.ini]

0 commit comments

Comments
 (0)