diff --git a/mypy/build.py b/mypy/build.py index b3ca8d06916d..ed7843e405c9 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -837,6 +837,8 @@ def parse_file( self.errors.ignored_files.add(path) tree = parse(source, path, id, self.errors, options=options) tree._fullname = id + if options.use_builtins_fixtures and id in CORE_BUILTIN_MODULES: + tree._is_typeshed_file = True self.add_stats( files_parsed=1, modules_parsed=int(not tree.is_stub), diff --git a/mypy/checker.py b/mypy/checker.py index 979a55b223c9..2bbb25dec351 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1066,6 +1066,9 @@ def check_func_item( If type_override is provided, use it as the function type. """ + if defn.is_synthetic(): + return + self.dynamic_funcs.append(defn.is_dynamic() and not type_override) with self.enter_partial_types(is_function=True): @@ -1324,9 +1327,7 @@ def check_func_def( ) # Ignore plugin generated methods, these usually don't need any bodies. - if defn.info is not FUNC_NO_INFO and ( - defn.name not in defn.info.names or defn.info.names[defn.name].plugin_generated - ): + if defn.is_synthetic(): show_error = False # Ignore also definitions that appear in `if TYPE_CHECKING: ...` blocks. @@ -4875,6 +4876,8 @@ def visit_del_stmt(self, s: DelStmt) -> None: ) def visit_decorator(self, e: Decorator) -> None: + if e.func.is_synthetic(): + return for d in e.decorators: if isinstance(d, RefExpr): if d.fullname == "typing.no_type_check": diff --git a/mypy/nodes.py b/mypy/nodes.py index 17e06613d1e3..81e242c68fbf 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -710,6 +710,12 @@ def __init__( if self.arguments[i] is None and i < self.max_fixed_argc(): self.min_args = i + 1 + def is_synthetic(self) -> bool: + if self.info is FUNC_NO_INFO: + return False + sym = self.info.names.get(self.name) + return not sym or sym.plugin_generated + def max_fixed_argc(self) -> int: return self.max_pos diff --git a/mypy/plugins/attrs.py b/mypy/plugins/attrs.py index 81f96c088ecd..dcd954606205 100644 --- a/mypy/plugins/attrs.py +++ b/mypy/plugins/attrs.py @@ -645,6 +645,11 @@ def _attribute_from_attrib_maker( rvalue, code=LITERAL_REQ, ) + + if init_type is None and ctx.api.options.disallow_untyped_defs: + assert lhs.node is not None + ctx.api.msg.need_annotation_for_var(lhs.node, stmt) + name = unmangle(lhs.name) return Attribute( name, alias, ctx.cls.info, attr_has_default, init, kw_only, converter_info, stmt, init_type diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index 107298875761..5b5221dfd58d 100644 --- a/test-data/unit/check-dataclasses.test +++ b/test-data/unit/check-dataclasses.test @@ -2544,3 +2544,18 @@ class Base: class Child(Base): y: int [builtins fixtures/dataclasses.pyi] + +[case testDataclassDisallowAny] +# flags: --disallow-any-explicit --disallow-any-decorated + +from dataclasses import dataclass +from typing import Any + +# Ensures that when Any-typed fields end up in synthetic functions, +# those functions are not flagged since there's nothing the user can do about that +@dataclass +class C: + a: Any # E: Explicit "Any" is not allowed + +[typing fixtures/typing-full.pyi] +[builtins fixtures/dataclasses.pyi] diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 04adaca317c1..3d8255629793 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -1303,18 +1303,18 @@ def f(i: int, s): main:3: error: Function is missing a return type annotation main:3: error: Function is missing a type annotation for one or more arguments -[case testDisallowIncompleteDefsAttrsNoAnnotations] -# flags: --disallow-incomplete-defs +[case testDisallowUntypedDefsAttrsNoAnnotations] +# flags: --disallow-untyped-defs import attrs @attrs.define class Unannotated: - foo = attrs.field() + foo = attrs.field() # E: Need type annotation for "foo" [builtins fixtures/plugin_attrs.pyi] -[case testDisallowIncompleteDefsAttrsWithAnnotations] -# flags: --disallow-incomplete-defs +[case testDisallowUntypedDefsAttrsWithAnnotations] +# flags: --disallow-untyped-defs import attrs @attrs.define @@ -1323,14 +1323,14 @@ class Annotated: [builtins fixtures/plugin_attrs.pyi] -[case testDisallowIncompleteDefsAttrsPartialAnnotations] -# flags: --disallow-incomplete-defs +[case testDisallowUntypedDefsAttrsPartialAnnotations] +# flags: --disallow-untyped-defs import attrs @attrs.define -class PartiallyAnnotated: # E: Function is missing a type annotation for one or more arguments +class PartiallyAnnotated: bar: int = attrs.field() - baz = attrs.field() + baz = attrs.field() # E: Need type annotation for "baz" [builtins fixtures/plugin_attrs.pyi]