diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index a8ef653ec8b3..6551e083816c 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -249,6 +249,35 @@ directory. The four possible values are: main.py:1: note: Import of 'submodule' ignored main.py:1: note: (Using --follow-imports=error, module not passed on command line) +Disallow Any Flags +***************************** +The ``--disallow-any`` family of flags disallows various types of ``Any`` in a module. +The following options are available: + +- ``--disallow-any-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. + +- ``--disallow-any-expr`` disallows all expressions in the module that have type ``Any``. + If an expression of type ``Any`` appears anywhere in the module + mypy will output an error unless the expression is immediately + used as an argument to ``cast`` or assigned to a variable with an + explicit type annotation. In addition, declaring a variable of type ``Any`` + or casting to type ``Any`` is not allowed. Note that calling functions + that take parameters of type ``Any`` is still allowed. + +- ``--disallow-any-decorated`` disallows functions that have ``Any`` in their signature + after decorator transformation. + +- ``--disallow-any-explicit`` disallows explicit ``Any`` in type positions such as type + annotations and generic type parameters. + +- ``--disallow-any-generics`` disallows usage of generic types that do not specify explicit + type parameters. Moreover, built-in collections (such as ``list`` and + ``dict``) become disallowed as you should use their aliases from the typing + module (such as ``List[int]`` and ``Dict[str, str]``). + Additional command line flags ***************************** @@ -277,42 +306,6 @@ 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``, ``expr``, ``decorated``, ``explicit``, - ``generics``. - - ``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``. - - ``expr`` disallows all expressions in the module that have type ``Any``. - If an expression of type ``Any`` appears anywhere in the module - mypy will output an error unless the expression is immediately - used as an argument to ``cast`` or assigned to a variable with an - explicit type annotation. In addition, declaring a variable of type ``Any`` - or casting to type ``Any`` is not allowed. Note that calling functions - that take parameters of type ``Any`` is still allowed. - - ``decorated`` disallows functions that have ``Any`` in their signature - after decorator transformation. - - ``explicit`` disallows explicit ``Any`` in type positions such as type - annotations and generic type parameters. - - ``generics`` disallows usage of generic types that do not specify explicit - type parameters. Moreover, built-in collections (such as ``list`` and - ``dict``) become disallowed as you should use their aliases from the typing - module (such as ``List[int]`` and ``Dict[str, str]``). - - ``--disallow-untyped-defs`` reports an error whenever it encounters a function definition without type annotations. diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index 9f5c832387f1..075e2f3ba8d8 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -156,11 +156,20 @@ 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``, ``expr``, ``decorated``, ``explicit``, ``generics``. - For explanations see the discussion for the :ref:`--disallow-any ` option. +- ``disallow_any_unimported`` (Boolean, default false) disallows usage of types that come + from unfollowed imports (such types become aliases for ``Any``). + +- ``disallow_any_expr`` (Boolean, default false) disallows all expressions in the module + that have type ``Any``. + +- ``disallow_any_decorated`` (Boolean, default false) disallows functions that have ``Any`` + in their signature after decorator transformation. + +- ``disallow_any_explicit`` (Boolean, default false) disallows explicit ``Any`` in type + positions such as type annotations and generic type parameters. + +- ``disallow_any_generics`` (Boolean, default false) disallows usage of generic types that + do not specify explicit type parameters. - ``disallow_subclassing_any`` (Boolean, default False) disallows subclassing a value of type ``Any``. See diff --git a/mypy/checker.py b/mypy/checker.py index a137fcab737d..7c0ac3268842 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -624,7 +624,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str]) item) self.check_for_missing_annotations(fdef) - if 'unimported' in self.options.disallow_any: + if self.options.disallow_any_unimported: if fdef.type and isinstance(fdef.type, CallableType): ret_type = fdef.type.ret_type if has_any_from_unimported_type(ret_type): @@ -1312,7 +1312,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None: self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax) if (s.type is not None and - 'unimported' in self.options.disallow_any and + self.options.disallow_any_unimported and has_any_from_unimported_type(s.type)): if isinstance(s.lvalues[-1], TupleExpr): # This is a multiple assignment. Instead of figuring out which type is problematic, @@ -2524,7 +2524,7 @@ def visit_with_stmt(self, s: WithStmt) -> None: self.accept(s.body) def check_untyped_after_decorator(self, typ: Type, func: FuncDef) -> None: - if 'decorated' not in self.options.disallow_any or self.is_stub: + if not self.options.disallow_any_decorated or self.is_stub: return if mypy.checkexpr.has_any_type(typ): diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 38834c4d4455..d8baf6a99b87 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -1786,7 +1786,7 @@ def visit_cast_expr(self, expr: CastExpr) -> Type: options = self.chk.options if options.warn_redundant_casts and is_same_type(source_type, target_type): self.msg.redundant_cast(target_type, expr) - if 'unimported' in options.disallow_any and has_any_from_unimported_type(target_type): + if options.disallow_any_unimported and has_any_from_unimported_type(target_type): self.msg.unimported_type_becomes_any("Target type of cast", target_type, expr) check_for_explicit_any(target_type, self.chk.options, self.chk.is_typeshed_stub, self.msg, context=expr) @@ -2379,7 +2379,7 @@ def accept(self, assert typ is not None self.chk.store_type(node, typ) - if ('expr' in self.chk.options.disallow_any and + if (self.chk.options.disallow_any_expr and not always_allow_any and not self.chk.is_stub and self.chk.in_checked_function() and @@ -2558,7 +2558,7 @@ def visit_newtype_expr(self, e: NewTypeExpr) -> Type: def visit_namedtuple_expr(self, e: NamedTupleExpr) -> Type: tuple_type = e.info.tuple_type if tuple_type: - if ('unimported' in self.chk.options.disallow_any and + if (self.chk.options.disallow_any_unimported and has_any_from_unimported_type(tuple_type)): self.msg.unimported_type_becomes_any("NamedTuple type", tuple_type, e) check_for_explicit_any(tuple_type, self.chk.options, self.chk.is_typeshed_stub, diff --git a/mypy/main.py b/mypy/main.py index 9de3054a927e..7460132d4a35 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -119,24 +119,6 @@ def type_check_only(sources: List[BuildSource], bin_dir: Optional[str], options=options) -disallow_any_options = ['unimported', 'expr', 'unannotated', 'decorated', 'explicit', 'generics'] - - -def disallow_any_argument_type(raw_options: str) -> List[str]: - if not raw_options: - # empty string disables all options - return [] - flag_options = [o.strip() for o in raw_options.split(',')] - for option in flag_options: - if option not in disallow_any_options: - formatted_valid_options = ', '.join( - "'{}'".format(o) for o in disallow_any_options) - message = "Invalid '--disallow-any' option '{}' (valid options are: {}).".format( - option, formatted_valid_options) - raise argparse.ArgumentError(None, message) - return flag_options - - FOOTER = """environment variables: MYPYPATH additional module search path""" @@ -272,10 +254,18 @@ def add_invertible_flag(flag: str, help="silently ignore imports of missing modules") parser.add_argument('--follow-imports', choices=['normal', 'silent', 'skip', 'error'], default='normal', help="how to treat imports (default normal)") - parser.add_argument('--disallow-any', type=disallow_any_argument_type, default=[], - metavar='{{{}}}'.format(', '.join(disallow_any_options)), - help="disallow various types of Any in a module. Takes a comma-separated " - "list of options (defaults to all options disabled)") + parser.add_argument('--disallow-any-unimported', default=False, action='store_true', + help="disallow Any types resulting from unfollowed imports") + parser.add_argument('--disallow-any-expr', default=False, action='store_true', + help='disallow all expressions that have type Any') + parser.add_argument('--disallow-any-decorated', default=False, action='store_true', + help='disallow functions that have Any in their signature ' + 'after decorator transformation') + parser.add_argument('--disallow-any-explicit', default=False, action='store_true', + help='disallow explicit Any in type positions') + parser.add_argument('--disallow-any-generics', default=False, action='store_true', + help='disallow usage of generic types that do not specify explicit ' + 'type parameters') add_invertible_flag('--disallow-untyped-calls', default=False, strict_flag=True, help="disallow calling functions without type annotations" " from functions with type annotations") @@ -364,6 +354,8 @@ def add_invertible_flag(flag: str, # --dump-graph will dump the contents of the graph of SCCs and exit. parser.add_argument('--dump-graph', action='store_true', help=argparse.SUPPRESS) # deprecated options + parser.add_argument('--disallow-any', dest='special-opts:disallow_any', + help=argparse.SUPPRESS) add_invertible_flag('--strict-boolean', default=False, help=argparse.SUPPRESS) parser.add_argument('-f', '--dirty-stubs', action='store_true', @@ -438,6 +430,9 @@ def add_invertible_flag(flag: str, ) # Process deprecated options + if special_opts.disallow_any: + print("--disallow-any option was split up into multiple flags. " + "See http://mypy.readthedocs.io/en/latest/command_line.html#disallow-any-flags") if options.strict_boolean: print("Warning: --strict-boolean is deprecated; " "see https://github.com/python/mypy/issues/3195", file=sys.stderr) @@ -462,9 +457,6 @@ 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, @@ -652,7 +644,6 @@ def get_init_file(dir: str) -> Optional[str]: 'custom_typeshed_dir': str, 'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)], 'junit_xml': str, - 'disallow_any': disallow_any_argument_type, # These two are for backwards compatibility 'silent_imports': bool, 'almost_silent': bool, @@ -770,14 +761,6 @@ 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': - # "disallow_any = " should disable all disallow_any options, including untyped defs, - # given in a more general config. - if not v: - results['disallow_untyped_defs'] = False - # If "unannotated" is explicitly given, turn on disallow_untyped_defs. - elif 'unannotated' in v: - results['disallow_untyped_defs'] = True if key == 'silent_imports': print("%s: silent_imports has been replaced by " "ignore_missing_imports=True; follow_imports=skip" % prefix, file=sys.stderr) diff --git a/mypy/options.py b/mypy/options.py index c6ee84c1f64f..b2092044d127 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -20,7 +20,11 @@ class Options: PER_MODULE_OPTIONS = { "ignore_missing_imports", "follow_imports", - "disallow_any", + "disallow_any_generics", + "disallow_any_unimported", + "disallow_any_expr", + "disallow_any_decorated", + "disallow_any_explicit", "disallow_subclassing_any", "disallow_untyped_calls", "disallow_untyped_defs", @@ -51,7 +55,13 @@ def __init__(self) -> None: self.report_dirs = {} # type: Dict[str, str] self.ignore_missing_imports = False self.follow_imports = 'normal' # normal|silent|skip|error - self.disallow_any = [] # type: List[str] + + # disallow_any options + self.disallow_any_generics = False + self.disallow_any_unimported = False + self.disallow_any_expr = False + self.disallow_any_decorated = False + self.disallow_any_explicit = False # Disallow calling untyped functions from typed ones self.disallow_untyped_calls = False diff --git a/mypy/semanal.py b/mypy/semanal.py index 283077a60763..3adf488d7abc 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1053,7 +1053,7 @@ def analyze_base_classes(self, defn: ClassDef) -> None: else: self.fail('Invalid base class', base_expr) info.fallback_to_any = True - if 'unimported' in self.options.disallow_any and has_any_from_unimported_type(base): + if self.options.disallow_any_unimported and has_any_from_unimported_type(base): if isinstance(base_expr, (NameExpr, MemberExpr)): prefix = "Base type {}".format(base_expr.name) else: @@ -2004,7 +2004,7 @@ def process_newtype_declaration(self, s: AssignmentStmt) -> None: check_for_explicit_any(old_type, self.options, self.is_typeshed_stub_file, self.msg, context=s) - if 'unimported' in self.options.disallow_any and has_any_from_unimported_type(old_type): + if self.options.disallow_any_unimported and has_any_from_unimported_type(old_type): self.msg.unimported_type_becomes_any("Argument 2 to NewType(...)", old_type, s) # If so, add it to the symbol table. @@ -2118,7 +2118,7 @@ def process_typevar_declaration(self, s: AssignmentStmt) -> None: return variance, upper_bound = res - if 'unimported' in self.options.disallow_any: + if self.options.disallow_any_unimported: for idx, constraint in enumerate(values, start=1): if has_any_from_unimported_type(constraint): prefix = "Constraint {}".format(idx) @@ -2587,7 +2587,7 @@ def parse_typeddict_args(self, call: CallExpr) -> Tuple[List[str], List[Type], b check_for_explicit_any(t, self.options, self.is_typeshed_stub_file, self.msg, context=call) - if 'unimported' in self.options.disallow_any: + if self.options.disallow_any_unimported: for t in types: if has_any_from_unimported_type(t): self.msg.unimported_type_becomes_any("Type of a TypedDict key", t, dictexpr) diff --git a/mypy/semanal_pass3.py b/mypy/semanal_pass3.py index 52c5bde83dbd..4ecb0961151e 100644 --- a/mypy/semanal_pass3.py +++ b/mypy/semanal_pass3.py @@ -372,7 +372,7 @@ def make_type_analyzer(self, indicator: Dict[str, bool]) -> TypeAnalyserPass3: indicator) def check_for_omitted_generics(self, typ: Type) -> None: - if 'generics' not in self.options.disallow_any or self.is_typeshed_file: + if not self.options.disallow_any_generics or self.is_typeshed_file: return for t in collect_any_types(typ): diff --git a/mypy/typeanal.py b/mypy/typeanal.py index fc211c4c10fc..2d2c5caad6c8 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -213,7 +213,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type: elif fullname == 'typing.Tuple': if len(t.args) == 0 and not t.empty_tuple_index: # Bare 'Tuple' is same as 'tuple' - if 'generics' in self.options.disallow_any and not self.is_typeshed_stub: + if self.options.disallow_any_generics and not self.is_typeshed_stub: self.fail(messages.BARE_GENERIC, t) typ = self.named_type('builtins.tuple', line=t.line, column=t.column) typ.from_generic_builtin = True @@ -669,7 +669,7 @@ def visit_instance(self, t: Instance) -> None: if len(t.args) != len(info.type_vars): if len(t.args) == 0: from_builtins = t.type.fullname() in nongen_builtins and not t.from_generic_builtin - if ('generics' in self.options.disallow_any and + if (self.options.disallow_any_generics and not self.is_typeshed_stub and from_builtins): alternative = nongen_builtins[t.type.fullname()] @@ -930,7 +930,7 @@ def check_for_explicit_any(typ: Optional[Type], is_typeshed_stub: bool, msg: MessageBuilder, context: Context) -> None: - if ('explicit' in options.disallow_any and + if (options.disallow_any_explicit and not is_typeshed_stub and typ and has_explicit_any(typ)): diff --git a/mypy/types.py b/mypy/types.py index c6d71869dcdd..3e5165946aa6 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -277,7 +277,7 @@ def TypeOfAny(x: str) -> str: unannotated = TypeOfAny('unannotated') # Does this Any come from an explicit type annotation? explicit = TypeOfAny('explicit') - # Does this come from an unfollowed import? See --disallow-any=unimported option + # Does this come from an unfollowed import? See --disallow-any-unimported option from_unimported_type = TypeOfAny('from_unimported_type') # Does this Any type come from omitted generics? from_omitted_generics = TypeOfAny('from_omitted_generics') diff --git a/mypy_self_check.ini b/mypy_self_check.ini index de05b8f8ce58..1e9c55a4d311 100644 --- a/mypy_self_check.ini +++ b/mypy_self_check.ini @@ -4,7 +4,8 @@ disallow_subclassing_any = True warn_no_return = True strict_optional = True no_implicit_optional = True -disallow_any = generics, unimported +disallow_any_generics = True +disallow_any_unimported = True warn_redundant_casts = True warn_unused_ignores = True warn_unused_configs = True diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 37c2bbebc485..6cffc8ec7e91 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -113,13 +113,6 @@ def d3(p) -> Any: @d1 # E: Untyped decorator makes function "f" untyped def f() -> None: pass -[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 from typing import Any @@ -570,14 +563,14 @@ strict_optional = False strict_optional = True [case testDisallowImplicitTypesIgnoreMissingTypes] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from missing import MyType def f(x: MyType) -> None: # E: Argument 1 to "f" becomes "Any" due to an unfollowed import pass [case testDisallowImplicitTypes] -# flags: --disallow-any=unimported +# flags: --disallow-any-unimported from missing import MyType def f(x: MyType) -> None: @@ -588,13 +581,13 @@ main:2: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" main:4: error: Argument 1 to "f" becomes "Any" due to an unfollowed import [case testDisallowImplicitAnyVariableDefinition] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from missing import Unchecked t: Unchecked = 12 # E: Type of variable becomes "Any" due to an unfollowed import [case testDisallowImplicitAnyGeneric] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from missing import Unchecked from typing import List @@ -608,7 +601,7 @@ main:5: error: Argument 1 to "foo" becomes "List[Any]" due to an unfollowed impo main:6: error: Type of variable becomes "List[Any]" due to an unfollowed import [case testDisallowImplicitAnyInherit] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from missing import Unchecked from typing import List @@ -620,7 +613,7 @@ class A(List[Unchecked]): # E: Base type becomes "List[Any]" due to an unfollowe [builtins fixtures/list.pyi] [case testDisallowImplicitAnyAlias] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from missing import Unchecked from typing import List @@ -631,7 +624,7 @@ def f(x: X) -> None: # E: Argument 1 to "f" becomes "List[Any]" due to an unfol [builtins fixtures/list.pyi] [case testDisallowImplicitAnyCast] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from missing import Unchecked from typing import List, cast @@ -642,7 +635,7 @@ cast(Unchecked, foo) # E: Target type of cast becomes "Any" due to an unfollowe [builtins fixtures/list.pyi] [case testDisallowImplicitAnyNamedTuple] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from typing import List, NamedTuple from missing import Unchecked @@ -653,7 +646,7 @@ Point = NamedTuple('Point', [('x', List[Unchecked]), main:5: error: NamedTuple type becomes "Tuple[List[Any], Any]" due to an unfollowed import [case testDisallowImplicitAnyTypeVarConstraints] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from typing import List, NamedTuple, TypeVar, Any from missing import Unchecked @@ -664,7 +657,7 @@ main:5: error: Constraint 1 becomes "Any" due to an unfollowed import main:5: error: Constraint 2 becomes "List[Any]" due to an unfollowed import [case testDisallowImplicitAnyNewType] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from typing import NewType, List from missing import Unchecked @@ -674,7 +667,7 @@ Bar = NewType('Bar', List[Unchecked]) # E: Argument 2 to NewType(...) becomes " [builtins fixtures/list.pyi] [case testDisallowImplicitAnyCallableAndTuple] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from typing import Callable, Tuple from missing import Unchecked @@ -686,14 +679,14 @@ main:5: error: Return type becomes "Tuple[Any]" due to an unfollowed import main:5: error: Argument 1 to "foo" becomes "Callable[[], Any]" due to an unfollowed import [case testDisallowImplicitAnySubclassingExplicitAny] -# flags: --ignore-missing-imports --disallow-any=unimported --disallow-subclassing-any +# flags: --ignore-missing-imports --disallow-any-unimported --disallow-subclassing-any from typing import Any class C(Any): # E: Class cannot subclass 'Any' (has type 'Any') pass [case testDisallowImplicitAnyVarDeclaration] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from missing import Unchecked foo: Unchecked = "" @@ -704,7 +697,7 @@ main:4: error: Type of variable becomes "Any" due to an unfollowed import main:6: error: A type on this line becomes "Any" due to an unfollowed import [case testDisallowUnimportedAnyTypedDictSimple] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from mypy_extensions import TypedDict from x import Unchecked @@ -714,7 +707,7 @@ def f(m: M) -> M: pass # no error [builtins fixtures/dict.pyi] [case testDisallowUnimportedAnyTypedDictGeneric] -# flags: --ignore-missing-imports --disallow-any=unimported +# flags: --ignore-missing-imports --disallow-any-unimported from mypy_extensions import TypedDict from typing import List @@ -726,7 +719,7 @@ def f(m: M) -> M: pass # no error [builtins fixtures/dict.pyi] [case testDisallowAnyDecoratedUnannotatedDecorator] -# flags: --disallow-any=decorated +# flags: --disallow-any-decorated from typing import Any def d(f): @@ -740,7 +733,7 @@ def h(x): # E: Function is untyped after decorator transformation pass [builtins fixtures/list.pyi] [case testDisallowAnyDecoratedErrorIsReportedOnlyOnce] -# flags: --disallow-any=decorated +# flags: --disallow-any-decorated def d(f): return f @@ -753,7 +746,7 @@ def d2(f): @d def f(x: int) -> None: pass # E: Function is untyped after decorator transformation [case testDisallowAnyDecoratedReturnAny] -# flags: --disallow-any=decorated +# flags: --disallow-any-decorated from typing import Any def d(f) -> Any: @@ -763,7 +756,7 @@ def d(f) -> Any: def f() -> None: pass # E: Function is untyped after decorator transformation [builtins fixtures/list.pyi] [case testDisallowAnyDecoratedReturnCallable] -# flags: --disallow-any=decorated +# flags: --disallow-any-decorated from typing import Any, Callable def d(f) -> Callable[..., None]: @@ -774,7 +767,7 @@ def g(i: int, s: str) -> None: pass # E: Type of decorated function contains ty [builtins fixtures/list.pyi] [case testDisallowAnyDecoratedNonexistentDecorator] -# flags: --disallow-any=decorated --ignore-missing-imports +# flags: --disallow-any-decorated --ignore-missing-imports from nonexistent import d @d @@ -782,7 +775,7 @@ def f() -> None: pass # E: Function is untyped after decorator transformation [builtins fixtures/list.pyi] [case testDisallowAnyDecoratedPartlyTypedCallable] -# flags: --disallow-any=decorated --ignore-missing-imports +# flags: --disallow-any-decorated --ignore-missing-imports from typing import Callable, Any, List def d(f) -> Callable[[int, Any], Any]: pass @@ -801,7 +794,7 @@ def h(i: int) -> None: # E: Type of decorated function contains type "Any" ("Ca [builtins fixtures/list.pyi] [case testDisallowAnyDecoratedReturnsCallableNoParams] -# flags: --disallow-any=decorated +# flags: --disallow-any-decorated from typing import Callable def d(p) -> Callable[[], int]: @@ -813,7 +806,7 @@ def f(i): [builtins fixtures/list.pyi] [case testDisallowAnyDecoratedDecoratorReturnsNonCallable] -# flags: --disallow-any=decorated +# flags: --disallow-any-decorated def d(p) -> int: return p(0) @@ -822,14 +815,14 @@ def f(i): return i [case testDisallowAnyDecoratedUntypedUndecoratedFunction] -# flags: --disallow-any=decorated +# flags: --disallow-any-decorated from typing import Callable def f(i): # no error return i [case testDisallowAnyDecoratedTwoDecorators] -# flags: --disallow-any=decorated +# flags: --disallow-any-decorated from typing import Callable def typed_dec(f) -> Callable[[], int]: pass @@ -846,7 +839,7 @@ def g(): # E: Function is untyped after decorator transformation return i [case testDisallowAnyExprSimple] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from typing import Any def f(s): yield s @@ -865,7 +858,7 @@ f(f(0)) # E: Expression has type "Any" [builtins fixtures/list.pyi] [case testDisallowAnyExprUnannotatedFunction] -# flags: --disallow-any=expr +# flags: --disallow-any-expr def g(s): return s @@ -873,7 +866,7 @@ g(0) w: int = g(1) [case testDisallowAnyExprExplicitAnyParam] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from typing import Any, List def f(s: Any) -> None: pass @@ -889,7 +882,7 @@ g(['']) # E: Expression type contains "Any" (has type "List[Any]") [builtins fixtures/list.pyi] [case testDisallowAnyExprAllowsAnyInCast] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from typing import Any, cast class Foo: g: Any = 2 @@ -900,7 +893,7 @@ k = Foo.g # E: Expression has type "Any" [builtins fixtures/list.pyi] [case testDisallowAnyExprAllowsAnyInVariableAssignmentWithExplicitTypeAnnotation] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from typing import Any class Foo: g: Any = 2 @@ -912,7 +905,7 @@ n = Foo().g # type: Any # E: Expression has type "Any" [builtins fixtures/list.pyi] [case testDisallowAnyExprGeneric] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from typing import List l: List = [] @@ -921,7 +914,7 @@ k = l[0] # E: Expression type contains "Any" (has type "List[Any]") # E: Expre [builtins fixtures/list.pyi] [case testDisallowAnyExprTypeVar] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from typing import TypeVar T = TypeVar('T') # no error @@ -931,7 +924,7 @@ def f(t: T) -> T: [builtins fixtures/list.pyi] [case testDisallowAnyExprNamedTuple] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from typing import NamedTuple Point = NamedTuple('Point', [('x', int), ('y', int)]) # no error @@ -941,7 +934,7 @@ def origin() -> Point: [builtins fixtures/list.pyi] [case testDisallowAnyExprNewType] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from typing import NewType NT = NewType('NT', int) # no error @@ -951,7 +944,7 @@ def nt() -> NT: [builtins fixtures/list.pyi] [case testDisallowAnyExprEnum] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from enum import Enum E = Enum('E', '1, 2, 3') # no error @@ -959,7 +952,7 @@ def k(s: E) -> None: pass [builtins fixtures/list.pyi] [case testDisallowAnyExprTypedDict] -# flags: --disallow-any=expr +# flags: --disallow-any-expr from mypy_extensions import TypedDict Movie = TypedDict('Movie', {'name': str, 'year': int}) diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 8464fe00cf2e..9ed8e602e278 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -176,76 +176,6 @@ 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] @@ -595,7 +525,7 @@ python_version = 3.6 # cmd: mypy main.py [file mypy.ini] [[mypy] -disallow_any = unimported +disallow_any_unimported = True ignore_missing_imports = True [file main.py] from unreal import F @@ -604,24 +534,12 @@ def f(x: F) -> None: pass [out] main.py:3: error: Argument 1 to "f" becomes "Any" due to an unfollowed import -[case testDisallowAnyEmpty] -# cmd: mypy main.py -[file mypy.ini] -[[mypy] -disallow_any = -ignore_missing_imports = True -[file main.py] -from unreal import F - -def f(x: F) -> None: pass -[out] - [case testDisallowAnyExplicitDefSignature] # cmd: mypy m.py [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List @@ -646,7 +564,7 @@ m.py:9: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List @@ -666,7 +584,7 @@ m.py:5: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List @@ -680,7 +598,7 @@ m.py:2: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List @@ -700,7 +618,7 @@ m.py:6: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List @@ -722,7 +640,7 @@ m.py:4: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List, TypeVar, Tuple @@ -747,7 +665,7 @@ m.py:10: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List, cast @@ -765,7 +683,7 @@ m.py:5: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List, NamedTuple @@ -782,7 +700,7 @@ m.py:3: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List, TypeVar @@ -797,12 +715,12 @@ m.py:3: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from typing import Any, List, NewType -Baz = NewType('Baz', Any) # this error does not come from `--disallow-any=explicit` flag +Baz = NewType('Baz', Any) # this error does not come from `--disallow-any-explicit` flag Bar = NewType('Bar', List[Any]) [out] @@ -815,7 +733,7 @@ m.py:4: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from mypy_extensions import TypedDict @@ -833,7 +751,7 @@ m.py:4: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m*] -disallow_any = explicit +disallow_any_explicit = True [file m.py] from mypy_extensions import TypedDict @@ -849,7 +767,7 @@ m.py:4: error: Explicit "Any" is not allowed [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] from typing import Tuple @@ -870,7 +788,7 @@ m.py:8: error: Missing type parameters for generic type [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] from typing import Tuple, List @@ -885,7 +803,7 @@ m.py:3: error: Missing type parameters for generic type [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] from typing import Type, Any @@ -905,7 +823,7 @@ m.py:8: error: Missing type parameters for generic type [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] from typing import List @@ -922,7 +840,7 @@ m.py:5: error: Missing type parameters for generic type [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] from typing import List, TypeVar, Tuple @@ -946,7 +864,7 @@ m.py:11: error: Missing type parameters for generic type [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] from typing import List @@ -970,7 +888,7 @@ m.py:9: error: Missing type parameters for generic type [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] from typing import Generic, TypeVar, Any @@ -993,7 +911,7 @@ m.py:10: error: Missing type parameters for generic type [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] s = tuple([1, 2, 3]) # no error @@ -1015,7 +933,7 @@ m.py:7: error: Implicit generic "Any". Use 'typing.FrozenSet' and specify generi [file mypy.ini] [[mypy] [[mypy-m] -disallow_any = generics +disallow_any_generics = True [file m.py] from typing import Tuple, List, Dict, Set, FrozenSet @@ -1084,7 +1002,7 @@ a/b/c/d/e/__init__.py:1: error: "int" not callable [file mypy.ini] [[mypy] disallow_untyped_defs = True -disallow_any = generics +disallow_any_generics = True [file a.py] def get_tasks(self): return 'whatever'