Skip to content

Rename --disallow-untyped-defs to --disallow-any=unannotated #3513

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

Merged
merged 4 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,22 @@ Here are some more useful flags:
re-check your code without ``--strict-optional`` to ensure new type errors
are not introduced.

.. _disallow-any:

- ``--disallow-any`` disallows various types of ``Any`` in a module.
The option takes a comma-separated list of the following values:
``unimported``, ``unannotated``.

``unimported`` disallows usage of types that come from unfollowed imports
(such types become aliases for ``Any``). Unfollowed imports occur either
when the imported module does not exist or when ``--follow-imports=skip``
is set.

``unannotated`` disallows function definitions that are not fully
typed (i.e. that are missing an explicit type annotation for any
of the parameters or the return type). ``unannotated`` option is
interchangeable with ``--disallow-untyped-defs``.

- ``--disallow-untyped-defs`` reports an error whenever it encounters
a function definition without type annotations.

Expand Down
6 changes: 6 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ overridden by the pattern sections matching the module name.
- ``almost_silent`` (Boolean, deprecated) equivalent to
``follow_imports=skip``.

- ``disallow_any`` (Comma-separated list, default empty) is an option to
disallow various types of ``Any`` in a module. The flag takes a
comma-separated list of the following arguments: ``unimported``,
``unannotated``. For explanations see the discussion for the
:ref:`--disallow-any <disallow-any>` option.

- ``disallow_untyped_calls`` (Boolean, default False) disallows
calling functions without type annotations from functions with type
annotations.
Expand Down
8 changes: 6 additions & 2 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def type_check_only(sources: List[BuildSource], bin_dir: str, options: Options)
options=options)


disallow_any_options = ['unimported']
disallow_any_options = ['unimported', 'unannotated']


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

strict_flag_names = [] # type: List[str]
strict_flag_assignments = [] # type: List[Tuple[str, bool]]
disallow_any_options = ['unimported']

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

if 'unannotated' in options.disallow_any:
options.disallow_untyped_defs = True

# Check for invalid argument combinations.
if require_targets:
code_methods = sum(bool(c) for c in [special_opts.modules,
Expand Down Expand Up @@ -724,6 +726,8 @@ def parse_section(prefix: str, template: Options,
except ValueError as err:
print("%s: %s: %s" % (prefix, key, err), file=sys.stderr)
continue
if key == 'disallow_any':
results['disallow_untyped_defs'] = v and 'unannotated' in v
if key == 'silent_imports':
print("%s: silent_imports has been replaced by "
"ignore_missing_imports=True; follow_imports=skip" % prefix, file=sys.stderr)
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def f():
1 + "str"
[out]
main:2: error: Function is missing a type annotation
[case testUntypedDefDisallowUnannotated]
# flags: --disallow-any=unannotated
def f():
1 + "str"
[out]
main:2: error: Function is missing a type annotation

[case testSubclassingAny]
# flags: --disallow-subclassing-any
Expand Down
70 changes: 70 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,76 @@ z.py:1: error: Function is missing a type annotation
z.py:4: error: Call to untyped function "f" in typed context
x.py:1: error: Function is missing a type annotation

[case testPerFileConfigSectionUntypedWithDisallowUnannotated]
# cmd: mypy w.py x.py y.py z.py
[file mypy.ini]
[[mypy]
disallow_any = unannotated
[[mypy-y*]
disallow_any =
[[mypy-z*]
disallow_untyped_defs = True
[[mypy-w*]
disallow_untyped_defs = False
[file x.py]
def f(a):
pass
[file y.py]
def f(a):
pass
[file z.py]
def f(a):
pass
[file w.py]
def f(a):
pass
[out]
z.py:1: error: Function is missing a type annotation
x.py:1: error: Function is missing a type annotation

[case testPerFileConfigSectionDisallowUnannotatedWithUntyped]
# cmd: mypy x.py y.py z.py
[file mypy.ini]
[[mypy]
disallow_untyped_defs = True
[[mypy-y*]
disallow_any =
[[mypy-z*]
disallow_any = unannotated
[file x.py]
def f(a):
pass
[file y.py]
def f(a):
pass
[file z.py]
def f(a):
pass
[out]
z.py:1: error: Function is missing a type annotation
x.py:1: error: Function is missing a type annotation

[case testPerFileConfigSectionDisallowUnannotatedNoOverride]
# cmd: mypy x.py y.py z.py
[file mypy.ini]
[[mypy]
[[mypy-x*]
disallow_untyped_defs = True
[[mypy-z*]
disallow_any = unannotated
[file x.py]
def f(a):
pass
[file y.py]
def f(a):
pass
[file z.py]
def f(a):
pass
[out]
z.py:1: error: Function is missing a type annotation
x.py:1: error: Function is missing a type annotation

[case testPerFileConfigSectionMultipleMatches]
# cmd: mypy xx.py xy.py yx.py yy.py
[file mypy.ini]
Expand Down