diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000000000..73cc04be89868 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,2 @@ +[mypy] +check_untyped_defs = true diff --git a/mypy/report.py b/mypy/report.py index 39cd80ed38bf0..f90ca33337326 100644 --- a/mypy/report.py +++ b/mypy/report.py @@ -26,7 +26,7 @@ from mypy.version import __version__ try: - from lxml import etree # type: ignore[import-untyped] + from lxml import etree LXML_INSTALLED = True except ImportError: diff --git a/mypy/stubtest.py b/mypy/stubtest.py index ab29d9dca4b83..e000e06877ff0 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -337,10 +337,10 @@ def verify_mypyfile( if isinstance(runtime, Missing): yield Error(object_path, "is not present at runtime", stub, runtime) return - if not isinstance(runtime, types.ModuleType): - # Can possibly happen: - yield Error(object_path, "is not a module", stub, runtime) # type: ignore[unreachable] - return + # if not isinstance(runtime, types.ModuleType): + # Can possibly happen: + # yield Error(object_path, "is not a module", stub, runtime) + # return runtime_all_as_set: set[str] | None diff --git a/mypy/suggestions.py b/mypy/suggestions.py index 16e630bf8c6ed..3c164413e9d5f 100644 --- a/mypy/suggestions.py +++ b/mypy/suggestions.py @@ -651,13 +651,17 @@ def extract_from_decorator(self, node: Decorator) -> FuncDef | None: if not isinstance(typ, FunctionLike): return None for ct in typ.items: - if not ( - len(ct.arg_types) == 1 - and isinstance(ct.arg_types[0], TypeVarType) - and ct.arg_types[0] == ct.ret_type - ): + if len(ct.arg_types) != 1: return None + arg_type = get_proper_type(ct.arg_types[0]) + ret_type = get_proper_type(ct.ret_type) + + if isinstance(arg_type, TypeVarType) and arg_type == ret_type: + continue + + return None + return node.func def try_type(self, func: FuncDef, typ: ProperType) -> list[str]: diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index e6415ddff9062..9851c73edafa0 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -5,6 +5,9 @@ import os import re import sys +import types + +import pytest from mypy import build from mypy.build import Graph @@ -24,14 +27,14 @@ ) from mypy.test.update_data import update_testcase_output +lxml: types.ModuleType | None try: - import lxml # type: ignore[import-untyped] + import importlib + + lxml = importlib.import_module("lxml") except ImportError: lxml = None - -import pytest - # List of files that contain test case descriptions. # Includes all check-* files with the .test extension in the test-data/unit directory typecheck_files = find_test_files(pattern="check-*.test") @@ -55,8 +58,10 @@ class TypeCheckSuite(DataSuite): files = typecheck_files def run_case(self, testcase: DataDrivenTestCase) -> None: - if lxml is None and os.path.basename(testcase.file) == "check-reports.test": + if lxml is None: + # if os.path.basename(testcase.file) == "check-reports.test": pytest.skip("Cannot import lxml. Is it installed?") + # return incremental = ( "incremental" in testcase.name.lower() or "incremental" in testcase.file diff --git a/mypy/test/testcmdline.py b/mypy/test/testcmdline.py index 9bc02d3199640..a1f43968920a3 100644 --- a/mypy/test/testcmdline.py +++ b/mypy/test/testcmdline.py @@ -10,6 +10,7 @@ import re import subprocess import sys +import types from mypy.test.config import PREFIX, test_temp_dir from mypy.test.data import DataDrivenTestCase, DataSuite @@ -19,11 +20,15 @@ normalize_error_messages, ) +lxml: types.ModuleType | None try: - import lxml # type: ignore[import-untyped] + import importlib + + lxml = importlib.import_module("lxml") except ImportError: lxml = None + import pytest # Path to Python 3 interpreter @@ -38,7 +43,7 @@ class PythonCmdlineSuite(DataSuite): native_sep = True def run_case(self, testcase: DataDrivenTestCase) -> None: - if lxml is None and os.path.basename(testcase.file) == "reports.test": + if os.path.basename(testcase.file) == "reports.test" and lxml is None: pytest.skip("Cannot import lxml. Is it installed?") for step in [1] + sorted(testcase.output2): test_python_cmdline(testcase, step) diff --git a/mypy/test/testreports.py b/mypy/test/testreports.py index f638756ad8196..a23fe345571d6 100644 --- a/mypy/test/testreports.py +++ b/mypy/test/testreports.py @@ -2,13 +2,18 @@ from __future__ import annotations +import importlib.util import textwrap +import types from mypy.report import CoberturaPackage, get_line_rate from mypy.test.helpers import Suite, assert_equal +lxml: types.ModuleType | None try: - import lxml # type: ignore[import-untyped] + import importlib + + lxml = importlib.import_module("lxml") except ImportError: lxml = None @@ -23,7 +28,7 @@ def test_get_line_rate(self) -> None: @pytest.mark.skipif(lxml is None, reason="Cannot import lxml. Is it installed?") def test_as_xml(self) -> None: - import lxml.etree as etree # type: ignore[import-untyped] + import lxml.etree as etree cobertura_package = CoberturaPackage("foobar") cobertura_package.covered_lines = 21 diff --git a/mypy/test_decorator_suggestion.py b/mypy/test_decorator_suggestion.py new file mode 100644 index 0000000000000..1398eaed8fe22 --- /dev/null +++ b/mypy/test_decorator_suggestion.py @@ -0,0 +1,19 @@ +from typing import Callable, TypeVar +from typing_extensions import ParamSpec + +R = TypeVar("R") +P = ParamSpec("P") + + +def dec(f: Callable[P, R]) -> Callable[P, R]: + return f + + +@dec +def f() -> None: + print("hello world") + + +@dec +def foo() -> None: + print() diff --git a/mypy/typeshed/stdlib/_frozen_importlib.pyi b/mypy/typeshed/stdlib/_frozen_importlib.pyi index 3dbc8c6b52f0d..9d6f2d8adad9d 100644 --- a/mypy/typeshed/stdlib/_frozen_importlib.pyi +++ b/mypy/typeshed/stdlib/_frozen_importlib.pyi @@ -1,7 +1,7 @@ import importlib.abc import importlib.machinery import sys -import types +import types from _typeshed.importlib import LoaderProtocol from collections.abc import Mapping, Sequence from types import ModuleType diff --git a/mypy/typeshed/stdlib/_frozen_importlib_external.pyi b/mypy/typeshed/stdlib/_frozen_importlib_external.pyi index 386cf20808e4f..ddd1c5b49d92b 100644 --- a/mypy/typeshed/stdlib/_frozen_importlib_external.pyi +++ b/mypy/typeshed/stdlib/_frozen_importlib_external.pyi @@ -3,7 +3,7 @@ import _io import importlib.abc import importlib.machinery import sys -import types +import types from _typeshed import ReadableBuffer, StrOrBytesPath, StrPath from _typeshed.importlib import LoaderProtocol from collections.abc import Callable, Iterable, Iterator, Mapping, MutableSequence, Sequence diff --git a/mypy/typeshed/stdlib/_imp.pyi b/mypy/typeshed/stdlib/_imp.pyi index de3549a91da59..ee29ea63d3bfc 100644 --- a/mypy/typeshed/stdlib/_imp.pyi +++ b/mypy/typeshed/stdlib/_imp.pyi @@ -1,5 +1,5 @@ import sys -import types +import types from _typeshed import ReadableBuffer from importlib.machinery import ModuleSpec from typing import Any diff --git a/mypy/typeshed/stdlib/_interpreters.pyi b/mypy/typeshed/stdlib/_interpreters.pyi index caa1115e9d3d6..f54c4c67c64a1 100644 --- a/mypy/typeshed/stdlib/_interpreters.pyi +++ b/mypy/typeshed/stdlib/_interpreters.pyi @@ -1,4 +1,4 @@ -import types +import types from collections.abc import Callable, Mapping from typing import Final, Literal, SupportsIndex from typing_extensions import TypeAlias diff --git a/mypy/typeshed/stdlib/asyncio/unix_events.pyi b/mypy/typeshed/stdlib/asyncio/unix_events.pyi index abf5d7ffd6998..78db14ac0165f 100644 --- a/mypy/typeshed/stdlib/asyncio/unix_events.pyi +++ b/mypy/typeshed/stdlib/asyncio/unix_events.pyi @@ -1,5 +1,5 @@ import sys -import types +import types from _typeshed import StrPath from abc import ABCMeta, abstractmethod from collections.abc import Callable diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi index dc8ddb8fe7a83..df4872bca66ae 100644 --- a/mypy/typeshed/stdlib/builtins.pyi +++ b/mypy/typeshed/stdlib/builtins.pyi @@ -2,7 +2,7 @@ import _ast import _sitebuiltins import _typeshed import sys -import types +import types from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import ( AnyStr_co, diff --git a/mypy/typeshed/stdlib/codecs.pyi b/mypy/typeshed/stdlib/codecs.pyi index 579d09c66a1bc..a14547f5904bd 100644 --- a/mypy/typeshed/stdlib/codecs.pyi +++ b/mypy/typeshed/stdlib/codecs.pyi @@ -1,4 +1,4 @@ -import types +import types from _codecs import * from _typeshed import ReadableBuffer from abc import abstractmethod diff --git a/mypy/typeshed/stdlib/dataclasses.pyi b/mypy/typeshed/stdlib/dataclasses.pyi index 3d89b830352b5..05aa4b31ec0fc 100644 --- a/mypy/typeshed/stdlib/dataclasses.pyi +++ b/mypy/typeshed/stdlib/dataclasses.pyi @@ -1,6 +1,6 @@ import enum import sys -import types +import types from _typeshed import DataclassInstance from builtins import type as Type # alias to avoid name clashes with fields named "type" from collections.abc import Callable, Iterable, Mapping diff --git a/mypy/typeshed/stdlib/dis.pyi b/mypy/typeshed/stdlib/dis.pyi index cb69eac89c920..b38aaf1c8adc1 100644 --- a/mypy/typeshed/stdlib/dis.pyi +++ b/mypy/typeshed/stdlib/dis.pyi @@ -1,5 +1,5 @@ import sys -import types +import types from collections.abc import Callable, Iterator from opcode import * # `dis` re-exports it as a part of public API from typing import IO, Any, NamedTuple diff --git a/mypy/typeshed/stdlib/doctest.pyi b/mypy/typeshed/stdlib/doctest.pyi index 562b5a5bdac9e..eac2a0ac944da 100644 --- a/mypy/typeshed/stdlib/doctest.pyi +++ b/mypy/typeshed/stdlib/doctest.pyi @@ -1,5 +1,5 @@ import sys -import types +import types import unittest from _typeshed import ExcInfo from collections.abc import Callable diff --git a/mypy/typeshed/stdlib/email/headerregistry.pyi b/mypy/typeshed/stdlib/email/headerregistry.pyi index dc641c8c952b3..a1960e2100e5e 100644 --- a/mypy/typeshed/stdlib/email/headerregistry.pyi +++ b/mypy/typeshed/stdlib/email/headerregistry.pyi @@ -1,4 +1,4 @@ -import types +import types from collections.abc import Iterable, Mapping from datetime import datetime as _datetime from email._header_value_parser import ( diff --git a/mypy/typeshed/stdlib/enum.pyi b/mypy/typeshed/stdlib/enum.pyi index 4a6287a712afc..64cc2eb3bbad1 100644 --- a/mypy/typeshed/stdlib/enum.pyi +++ b/mypy/typeshed/stdlib/enum.pyi @@ -1,6 +1,6 @@ import _typeshed import sys -import types +import types from _typeshed import SupportsKeysAndGetItem, Unused from builtins import property as _builtins_property from collections.abc import Callable, Iterable, Iterator, Mapping diff --git a/mypy/typeshed/stdlib/functools.pyi b/mypy/typeshed/stdlib/functools.pyi index f786167e322d7..1cd87f2c41c23 100644 --- a/mypy/typeshed/stdlib/functools.pyi +++ b/mypy/typeshed/stdlib/functools.pyi @@ -1,5 +1,5 @@ import sys -import types +import types from _typeshed import SupportsAllComparisons, SupportsItems from collections.abc import Callable, Hashable, Iterable, Sized from typing import Any, Generic, Literal, NamedTuple, TypedDict, TypeVar, final, overload diff --git a/mypy/typeshed/stdlib/http/client.pyi b/mypy/typeshed/stdlib/http/client.pyi index cd2fc4f5a652a..bccccd725f162 100644 --- a/mypy/typeshed/stdlib/http/client.pyi +++ b/mypy/typeshed/stdlib/http/client.pyi @@ -2,7 +2,7 @@ import email.message import io import ssl import sys -import types +import types from _typeshed import MaybeNone, ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer from collections.abc import Callable, Iterable, Iterator, Mapping from socket import socket diff --git a/mypy/typeshed/stdlib/imp.pyi b/mypy/typeshed/stdlib/imp.pyi index ee5a0cd7bc726..ff0d9ba7d8b49 100644 --- a/mypy/typeshed/stdlib/imp.pyi +++ b/mypy/typeshed/stdlib/imp.pyi @@ -1,4 +1,4 @@ -import types +import types from _imp import ( acquire_lock as acquire_lock, create_dynamic as create_dynamic, diff --git a/mypy/typeshed/stdlib/importlib/_abc.pyi b/mypy/typeshed/stdlib/importlib/_abc.pyi index 1a21b9a72cd85..c8305a436302e 100644 --- a/mypy/typeshed/stdlib/importlib/_abc.pyi +++ b/mypy/typeshed/stdlib/importlib/_abc.pyi @@ -1,5 +1,5 @@ import sys -import types +import types from abc import ABCMeta from importlib.machinery import ModuleSpec diff --git a/mypy/typeshed/stdlib/importlib/abc.pyi b/mypy/typeshed/stdlib/importlib/abc.pyi index 588377d7d8718..6708d45ed2124 100644 --- a/mypy/typeshed/stdlib/importlib/abc.pyi +++ b/mypy/typeshed/stdlib/importlib/abc.pyi @@ -1,6 +1,6 @@ import _ast import sys -import types +import types from _typeshed import ReadableBuffer, StrPath from abc import ABCMeta, abstractmethod from collections.abc import Iterator, Mapping, Sequence diff --git a/mypy/typeshed/stdlib/importlib/metadata/__init__.pyi b/mypy/typeshed/stdlib/importlib/metadata/__init__.pyi index 8ab7a0c4a9e8f..b3f9272099eff 100644 --- a/mypy/typeshed/stdlib/importlib/metadata/__init__.pyi +++ b/mypy/typeshed/stdlib/importlib/metadata/__init__.pyi @@ -1,7 +1,7 @@ import abc import pathlib import sys -import types +import types from _collections_abc import dict_keys, dict_values from _typeshed import StrPath from collections.abc import Iterable, Iterator, Mapping diff --git a/mypy/typeshed/stdlib/importlib/resources/_common.pyi b/mypy/typeshed/stdlib/importlib/resources/_common.pyi index d6a9436544dce..d9e916422a5f8 100644 --- a/mypy/typeshed/stdlib/importlib/resources/_common.pyi +++ b/mypy/typeshed/stdlib/importlib/resources/_common.pyi @@ -2,7 +2,7 @@ import sys # Even though this file is 3.11+ only, Pyright will complain in stubtest for older versions. if sys.version_info >= (3, 11): - import types + import types from collections.abc import Callable from contextlib import AbstractContextManager from importlib.abc import ResourceReader, Traversable diff --git a/mypy/typeshed/stdlib/importlib/util.pyi b/mypy/typeshed/stdlib/importlib/util.pyi index cc1c98ae4d0e4..26fa7f50ca40f 100644 --- a/mypy/typeshed/stdlib/importlib/util.pyi +++ b/mypy/typeshed/stdlib/importlib/util.pyi @@ -1,7 +1,7 @@ import importlib.abc import importlib.machinery import sys -import types +import types from _typeshed import ReadableBuffer from collections.abc import Callable from importlib._bootstrap import module_from_spec as module_from_spec, spec_from_loader as spec_from_loader diff --git a/mypy/typeshed/stdlib/inspect.pyi b/mypy/typeshed/stdlib/inspect.pyi index 5bebe9bf44826..99629fd6bc287 100644 --- a/mypy/typeshed/stdlib/inspect.pyi +++ b/mypy/typeshed/stdlib/inspect.pyi @@ -1,7 +1,7 @@ import dis import enum import sys -import types +import types from _typeshed import StrPath from collections import OrderedDict from collections.abc import AsyncGenerator, Awaitable, Callable, Coroutine, Generator, Mapping, Sequence, Set as AbstractSet diff --git a/mypy/typeshed/stdlib/marshal.pyi b/mypy/typeshed/stdlib/marshal.pyi index 6ab202637ddaa..22867b33217fd 100644 --- a/mypy/typeshed/stdlib/marshal.pyi +++ b/mypy/typeshed/stdlib/marshal.pyi @@ -1,6 +1,6 @@ import builtins import sys -import types +import types from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite from typing import Any from typing_extensions import TypeAlias diff --git a/mypy/typeshed/stdlib/pathlib.pyi b/mypy/typeshed/stdlib/pathlib.pyi index a18aed4ba57a9..4b241ef117773 100644 --- a/mypy/typeshed/stdlib/pathlib.pyi +++ b/mypy/typeshed/stdlib/pathlib.pyi @@ -1,5 +1,5 @@ import sys -import types +import types from _typeshed import ( OpenBinaryMode, OpenBinaryModeReading, diff --git a/mypy/typeshed/stdlib/socketserver.pyi b/mypy/typeshed/stdlib/socketserver.pyi index 061932f0fac7e..8218b279e5572 100644 --- a/mypy/typeshed/stdlib/socketserver.pyi +++ b/mypy/typeshed/stdlib/socketserver.pyi @@ -1,5 +1,5 @@ import sys -import types +import types from _socket import _Address, _RetAddress from _typeshed import ReadableBuffer from collections.abc import Callable diff --git a/mypy/typeshed/stdlib/trace.pyi b/mypy/typeshed/stdlib/trace.pyi index 04390f1191951..a8907eddeda32 100644 --- a/mypy/typeshed/stdlib/trace.pyi +++ b/mypy/typeshed/stdlib/trace.pyi @@ -1,5 +1,5 @@ import sys -import types +import types from _typeshed import Incomplete, StrPath, TraceFunction from collections.abc import Callable, Iterable, Mapping, Sequence from typing import Any, TypeVar diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index bd59dfbdfd5ee..3888991721858 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -1831,7 +1831,7 @@ F = Callable[[Arg(int, 'x')], int] # E: Invalid argument constructor "__main__. from typing import Callable, List from mypy_extensions import Arg, VarArg, KwArg import mypy_extensions -import types # Needed for type checking +import types # Needed for type checking def WrongArg(x, y): return y # Note that for this test, the 'Value of type "int" is not indexable' errors are silly, diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index 2f3d5e08dab37..16a926f7da931 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -1671,7 +1671,7 @@ x: Comparable[Good] y: Comparable[int] # E: Type argument "int" of "Comparable" must be a subtype of "Comparable[Any]" [case testPEP695TypeAliasWithDifferentTargetTypes] -import types # We need GenericAlias from here, and test stubs don't bring in 'types' +import types # We need GenericAlias from here, and test stubs don't bring in 'types' from typing import Any, Callable, List, Literal, TypedDict # Test that various type expressions don't generate false positives as type alias diff --git a/test-data/unit/check-type-object-type-inference.test b/test-data/unit/check-type-object-type-inference.test index 5a4afa0c92481..f790ee9636b77 100644 --- a/test-data/unit/check-type-object-type-inference.test +++ b/test-data/unit/check-type-object-type-inference.test @@ -2,7 +2,7 @@ # flags: --python-version 3.9 from typing import TypeVar, Generic, Type from abc import abstractmethod -import types # Explicitly bring in stubs for 'types' +import types # Explicitly bring in stubs for 'types' T = TypeVar('T') class E(Generic[T]): diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index bf17c34b99a7b..c550dff7fb2d0 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -2075,7 +2075,7 @@ class F: def g(): ... [case testCoroutineImportTypesAsT] -import types as t +import types as t class F: @t.coroutine @@ -2086,7 +2086,7 @@ class F: def g(): return 2 [out] -import types as t +import types as t class F: @t.coroutine