cleanups and mypy 1.1.1 fixes (#5977)

This commit is contained in:
Maximilian Hils 2023-03-08 13:14:12 +01:00 committed by GitHub
parent c7de03bc36
commit 2b10e3f3a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 20 deletions

View file

@ -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

View file

@ -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):

View file

@ -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

View file

@ -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:

View file

@ -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.
"""

View file

@ -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