From 2b10e3f3a79aef4e4c077a4ea056ebb3bb493ddb Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 8 Mar 2023 13:14:12 +0100 Subject: [PATCH] cleanups and mypy 1.1.1 fixes (#5977) --- mitmproxy/certs.py | 13 +++++-------- mitmproxy/coretypes/serializable.py | 2 +- mitmproxy/hooks.py | 2 +- mitmproxy/proxy/mode_servers.py | 12 +++++++----- mitmproxy/proxy/mode_specs.py | 10 ++++++---- setup.cfg | 2 +- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/mitmproxy/certs.py b/mitmproxy/certs.py index ce6d9443c..7477c61f7 100644 --- a/mitmproxy/certs.py +++ b/mitmproxy/certs.py @@ -6,6 +6,7 @@ import re import sys from dataclasses import dataclass from pathlib import Path +from typing import cast from typing import NewType from typing import Optional from typing import Union @@ -134,7 +135,7 @@ class Cert(serializable.Serializable): def cn(self) -> str | None: attrs = self._cert.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME) if attrs: - return attrs[0].value + return cast(str, attrs[0].value) return None @property @@ -143,7 +144,7 @@ class Cert(serializable.Serializable): x509.NameOID.ORGANIZATION_NAME ) if attrs: - return attrs[0].value + return cast(str, attrs[0].value) return None @property @@ -166,12 +167,8 @@ class Cert(serializable.Serializable): def _name_to_keyval(name: x509.Name) -> list[tuple[str, str]]: parts = [] for attr in name: - # pyca cryptography <35.0.0 backwards compatiblity - if hasattr(name, "rfc4514_attribute_name"): # pragma: no cover - k = attr.rfc4514_attribute_name # type: ignore - else: # pragma: no cover - k = attr.rfc4514_string().partition("=")[0] - v = attr.value + k = attr.rfc4514_string().partition("=")[0] + v = cast(str, attr.value) parts.append((k, v)) return parts diff --git a/mitmproxy/coretypes/serializable.py b/mitmproxy/coretypes/serializable.py index f91d202c8..fc185e352 100644 --- a/mitmproxy/coretypes/serializable.py +++ b/mitmproxy/coretypes/serializable.py @@ -69,7 +69,7 @@ class SerializableDataclass(Serializable): hints = typing.get_type_hints(cls) fields = [] # noinspection PyDataclass - for field in dataclasses.fields(cls): + for field in dataclasses.fields(cls): # type: ignore[arg-type] if field.metadata.get("serialize", True) is False: continue if isinstance(field.type, str): diff --git a/mitmproxy/hooks.py b/mitmproxy/hooks.py index 4acb92669..870c6c208 100644 --- a/mitmproxy/hooks.py +++ b/mitmproxy/hooks.py @@ -20,7 +20,7 @@ class Hook: def args(self) -> list[Any]: args = [] - for field in fields(self): + for field in fields(self): # type: ignore[arg-type] args.append(getattr(self, field.name)) return args diff --git a/mitmproxy/proxy/mode_servers.py b/mitmproxy/proxy/mode_servers.py index 405348e6b..43213f91b 100644 --- a/mitmproxy/proxy/mode_servers.py +++ b/mitmproxy/proxy/mode_servers.py @@ -17,6 +17,7 @@ import json import logging import os import socket +import sys import textwrap import typing from abc import ABCMeta @@ -46,6 +47,11 @@ from mitmproxy.proxy.context import Context from mitmproxy.proxy.layer import Layer from mitmproxy.utils import human +if sys.version_info < (3, 11): + from typing_extensions import Self # pragma: no cover +else: + from typing import Self + logger = logging.getLogger(__name__) @@ -79,10 +85,6 @@ class ServerManager(typing.Protocol): ... # pragma: no cover -# Python 3.11: Use typing.Self -Self = TypeVar("Self", bound="ServerInstance") - - class ServerInstance(Generic[M], metaclass=ABCMeta): __modes: ClassVar[dict[str, type[ServerInstance]]] = {} @@ -103,7 +105,7 @@ class ServerInstance(Generic[M], metaclass=ABCMeta): @classmethod def make( - cls: type[Self], + cls, mode: mode_specs.ProxyMode | str, manager: ServerManager, ) -> Self: diff --git a/mitmproxy/proxy/mode_specs.py b/mitmproxy/proxy/mode_specs.py index 8bffb3b53..281c81d2e 100644 --- a/mitmproxy/proxy/mode_specs.py +++ b/mitmproxy/proxy/mode_specs.py @@ -22,21 +22,23 @@ Examples: from __future__ import annotations import dataclasses +import sys from abc import ABCMeta from abc import abstractmethod from dataclasses import dataclass from functools import cache from typing import ClassVar from typing import Literal -from typing import TypeVar import mitmproxy_rs from mitmproxy.coretypes.serializable import Serializable from mitmproxy.net import server_spec -# Python 3.11: Use typing.Self -Self = TypeVar("Self", bound="ProxyMode") +if sys.version_info < (3, 11): + from typing_extensions import Self # pragma: no cover +else: + from typing import Self @dataclass(frozen=True) # type: ignore @@ -92,7 +94,7 @@ class ProxyMode(Serializable, metaclass=ABCMeta): @classmethod @cache - def parse(cls: type[Self], spec: str) -> Self: + def parse(cls, spec: str) -> Self: """ Parse a proxy mode specification and return the corresponding `ProxyMode` instance. """ diff --git a/setup.cfg b/setup.cfg index 46f989024..df4764b93 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,7 +32,7 @@ exclude_lines = [mypy] check_untyped_defs = True ignore_missing_imports = True -files = mitmproxy,examples/addons,release +files = mitmproxy,examples/addons,release/*.py [mypy-mitmproxy.contrib.*] ignore_errors = True