Skip to content

Dmypy 418 #18942

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ generic types or your own type aliases), look through the

.. note::

When adding types, the convention is to import types
When adding types, the convention is to import types as std_types
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is talking about importing typing and this isn't the convention.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the convention is "never alias-import stdlib modules unless strictly necessary". Please revert this doc change along with all imports garbled in other files.

using the form ``from typing import <name>`` (as opposed to doing
just ``import typing`` or ``import typing as t`` or ``from typing import *``).

Expand Down
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mypy]
check_untyped_defs = true
4 changes: 2 additions & 2 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import stat
import sys
import time
import types
import types as std_types
from collections.abc import Iterator, Mapping, Sequence, Set as AbstractSet
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -511,7 +511,7 @@ def load_plugins(
return ChainedPlugin(options, custom_plugins + [default_plugin]), snapshot


def take_module_snapshot(module: types.ModuleType) -> str:
def take_module_snapshot(module: std_types.ModuleType) -> str:
"""Take plugin module snapshot by recording its version and hash.

We record _both_ hash and the version to detect more possible changes
Expand Down
2 changes: 1 addition & 1 deletion mypy/pyinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
if sys.version_info < (3, 11):
old_sys_path = sys.path
sys.path = sys.path[1:]
import types # noqa: F401
import types as std_types # noqa: F401

sys.path = old_sys_path

Expand Down
2 changes: 1 addition & 1 deletion mypy/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
TypeOfAny,
TypeStrVisitor,
TypeTranslator,
TypeVarType,
UninhabitedType,
UnionType,
get_proper_type,
Expand Down Expand Up @@ -653,7 +652,7 @@ def extract_from_decorator(self, node: Decorator) -> FuncDef | None:
for ct in typ.items:
if not (
len(ct.arg_types) == 1
and isinstance(ct.arg_types[0], TypeVarType)
# and isinstance(ct.arg_types[0], TypeVarType)
and ct.arg_types[0] == ct.ret_type
):
return None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looks like a special case for this decorator:

def f(x: T) -> T:
  return x

you should add a special case in addition to this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, technically this line is limiting to that special case. And this will even fix the linked issue, two callables of that shape will compare equal AFAIC.

But this also makes the following (completely wrong) deco generate a signature:

def deco(fn: str) -> str: ...

@deco
def foo():
    print()

So I agree there should be one more special case, not just blanket "one-arg function returning its arg unchanged".

Expand Down
20 changes: 15 additions & 5 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import os
import re
import sys
import types
from typing import cast

import lxml

from mypy import build
from mypy.build import Graph
Expand All @@ -24,10 +28,14 @@
)
from mypy.test.update_data import update_testcase_output

try:
import lxml # type: ignore[import-untyped]
except ImportError:
lxml = None
# try:
# import lxml as _lxml

# lxml: Optional[types.ModuleType] = _lxml
# except ImportError:
# lxml: Optional[types.ModuleType] = None

lxml = cast(types.ModuleType, lxml)


import pytest
Expand Down Expand Up @@ -55,8 +63,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
Expand Down
7 changes: 5 additions & 2 deletions mypy/test/testcmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,11 +20,13 @@
normalize_error_messages,
)

lxml: types.ModuleType | None
try:
import lxml # type: ignore[import-untyped]
import lxml
except ImportError:
lxml = None


import pytest

# Path to Python 3 interpreter
Expand All @@ -38,7 +41,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)
Expand Down
16 changes: 9 additions & 7 deletions mypy/test/testreports.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@

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

try:
import lxml # type: ignore[import-untyped]
except ImportError:
lxml = None
if importlib.util.find_spec("lxml") is None:
lxml2: types.ModuleType | None = None
else:
import lxml as lxml2

import pytest


class CoberturaReportSuite(Suite):
@pytest.mark.skipif(lxml is None, reason="Cannot import lxml. Is it installed?")
@pytest.mark.skipif(lxml2 is None, reason="Cannot import lxml. Is it installed?")
def test_get_line_rate(self) -> None:
assert_equal("1.0", get_line_rate(0, 0))
assert_equal("0.3333", get_line_rate(1, 3))

@pytest.mark.skipif(lxml is None, reason="Cannot import lxml. Is it installed?")
@pytest.mark.skipif(lxml2 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
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ def test_missing_no_runtime_all_terrible(self) -> Iterator[Case]:
stub="",
runtime="""
import sys
import types
import types as std_types
import __future__
_m = types.SimpleNamespace()
_m.annotations = __future__.annotations
Expand Down
14 changes: 14 additions & 0 deletions mypy/test_decorator_suggestion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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")
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_frozen_importlib.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import importlib.abc
import importlib.machinery
import sys
import types
import types as std_types
from _typeshed.importlib import LoaderProtocol
from collections.abc import Mapping, Sequence
from types import ModuleType
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_frozen_importlib_external.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _io
import importlib.abc
import importlib.machinery
import sys
import types
import types as std_types
from _typeshed import ReadableBuffer, StrOrBytesPath, StrPath
from _typeshed.importlib import LoaderProtocol
from collections.abc import Callable, Iterable, Iterator, Mapping, MutableSequence, Sequence
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_imp.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import types
import types as std_types
from _typeshed import ReadableBuffer
from importlib.machinery import ModuleSpec
from typing import Any
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_interpreters.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import types
import types as std_types
from collections.abc import Callable, Mapping
from typing import Final, Literal, SupportsIndex
from typing_extensions import TypeAlias
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/asyncio/unix_events.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import types
import types as std_types
from _typeshed import StrPath
from abc import ABCMeta, abstractmethod
from collections.abc import Callable
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ast
import _sitebuiltins
import _typeshed
import sys
import types
import types as std_types
from _collections_abc import dict_items, dict_keys, dict_values
from _typeshed import (
AnyStr_co,
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/codecs.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import types
import types as std_types
from _codecs import *
from _typeshed import ReadableBuffer
from abc import abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/dataclasses.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import enum
import sys
import types
import types as std_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
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/dis.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import types
import types as std_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
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/doctest.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import types
import types as std_types
import unittest
from _typeshed import ExcInfo
from collections.abc import Callable
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/email/headerregistry.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import types
import types as std_types
from collections.abc import Iterable, Mapping
from datetime import datetime as _datetime
from email._header_value_parser import (
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/enum.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _typeshed
import sys
import types
import types as std_types
from _typeshed import SupportsKeysAndGetItem, Unused
from builtins import property as _builtins_property
from collections.abc import Callable, Iterable, Iterator, Mapping
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/functools.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import types
import types as std_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
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/http/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import email.message
import io
import ssl
import sys
import types
import types as std_types
from _typeshed import MaybeNone, ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer
from collections.abc import Callable, Iterable, Iterator, Mapping
from socket import socket
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/imp.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import types
import types as std_types
from _imp import (
acquire_lock as acquire_lock,
create_dynamic as create_dynamic,
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/importlib/_abc.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import types
import types as std_types
from abc import ABCMeta
from importlib.machinery import ModuleSpec

Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/importlib/abc.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ast
import sys
import types
import types as std_types
from _typeshed import ReadableBuffer, StrPath
from abc import ABCMeta, abstractmethod
from collections.abc import Iterator, Mapping, Sequence
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/importlib/metadata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
import pathlib
import sys
import types
import types as std_types
from _collections_abc import dict_keys, dict_values
from _typeshed import StrPath
from collections.abc import Iterable, Iterator, Mapping
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/importlib/resources/_common.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 as std_types
from collections.abc import Callable
from contextlib import AbstractContextManager
from importlib.abc import ResourceReader, Traversable
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/importlib/util.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import importlib.abc
import importlib.machinery
import sys
import types
import types as std_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
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/inspect.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dis
import enum
import sys
import types
import types as std_types
from _typeshed import StrPath
from collections import OrderedDict
from collections.abc import AsyncGenerator, Awaitable, Callable, Coroutine, Generator, Mapping, Sequence, Set as AbstractSet
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/marshal.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import builtins
import sys
import types
import types as std_types
from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite
from typing import Any
from typing_extensions import TypeAlias
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/pathlib.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import types
import types as std_types
from _typeshed import (
OpenBinaryMode,
OpenBinaryModeReading,
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/socketserver.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import types
import types as std_types
from _socket import _Address, _RetAddress
from _typeshed import ReadableBuffer
from collections.abc import Callable
Expand Down
Loading
Loading