From 531bc9891fa04cbe0d052baac4e882c871626579 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Mon, 10 Jun 2024 18:18:33 -0700 Subject: [PATCH] Fix issues reported by mypy (#83413) --- lib/ansible/galaxy/collection/__init__.py | 2 +- .../collection/concrete_artifact_manager.py | 2 +- lib/ansible/galaxy/collection/gpg.py | 40 ++++++++----------- lib/ansible/parsing/yaml/constructor.py | 18 +++++---- test/sanity/ignore.txt | 9 ----- 5 files changed, 29 insertions(+), 42 deletions(-) diff --git a/lib/ansible/galaxy/collection/__init__.py b/lib/ansible/galaxy/collection/__init__.py index 47b9cb53af7..d2d8ae84713 100644 --- a/lib/ansible/galaxy/collection/__init__.py +++ b/lib/ansible/galaxy/collection/__init__.py @@ -1914,7 +1914,7 @@ def _resolve_depenency_map( for req in dep_exc.criterion.iter_requirement(): error_msg_lines.append( - '* {req.fqcn!s}:{req.ver!s}'.format(req=req) + f'* {req.fqcn!s}:{req.ver!s}' ) error_msg_lines.append(pre_release_hint) diff --git a/lib/ansible/galaxy/collection/concrete_artifact_manager.py b/lib/ansible/galaxy/collection/concrete_artifact_manager.py index 27ce287af3c..a67138fd2f4 100644 --- a/lib/ansible/galaxy/collection/concrete_artifact_manager.py +++ b/lib/ansible/galaxy/collection/concrete_artifact_manager.py @@ -413,7 +413,7 @@ def _extract_collection_from_git(repo_url, coll_ver, b_path): b_checkout_path = mkdtemp( dir=b_path, prefix=to_bytes(name, errors='surrogate_or_strict'), - ) # type: bytes + ) try: git_executable = get_bin_path('git') diff --git a/lib/ansible/galaxy/collection/gpg.py b/lib/ansible/galaxy/collection/gpg.py index 38ec189ddd0..9d41cdcde8c 100644 --- a/lib/ansible/galaxy/collection/gpg.py +++ b/lib/ansible/galaxy/collection/gpg.py @@ -12,20 +12,14 @@ import contextlib import inspect import os import subprocess -import sys import typing as t from dataclasses import dataclass, fields as dc_fields -from functools import partial from urllib.error import HTTPError, URLError if t.TYPE_CHECKING: from ansible.utils.display import Display -IS_PY310_PLUS = sys.version_info[:2] >= (3, 10) - -frozen_dataclass = partial(dataclass, frozen=True, **({'slots': True} if IS_PY310_PLUS else {})) - def get_signature_from_source(source, display=None): # type: (str, t.Optional[Display]) -> str if display is not None: @@ -128,7 +122,7 @@ def parse_gpg_errors(status_out): # type: (str) -> t.Iterator[GpgBaseError] yield cls(*fields) -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgBaseError(Exception): status: str @@ -142,35 +136,35 @@ class GpgBaseError(Exception): super(GpgBaseError, self).__setattr__(field_name, field_type(getattr(self, field_name))) -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgExpSig(GpgBaseError): """The signature with the keyid is good, but the signature is expired.""" keyid: str username: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgExpKeySig(GpgBaseError): """The signature with the keyid is good, but the signature was made by an expired key.""" keyid: str username: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgRevKeySig(GpgBaseError): """The signature with the keyid is good, but the signature was made by a revoked key.""" keyid: str username: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgBadSig(GpgBaseError): """The signature with the keyid has not been verified okay.""" keyid: str username: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgErrSig(GpgBaseError): """"It was not possible to check the signature. This may be caused by a missing public key or an unsupported algorithm. A RC of 4 @@ -186,24 +180,24 @@ class GpgErrSig(GpgBaseError): fpr: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgNoPubkey(GpgBaseError): """The public key is not available.""" keyid: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgMissingPassPhrase(GpgBaseError): """No passphrase was supplied.""" -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgBadPassphrase(GpgBaseError): """The supplied passphrase was wrong or not given.""" keyid: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgNoData(GpgBaseError): """No data has been found. Codes for WHAT are: - 1 :: No armored data. @@ -215,7 +209,7 @@ class GpgNoData(GpgBaseError): what: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgUnexpected(GpgBaseError): """No data has been found. Codes for WHAT are: - 1 :: No armored data. @@ -227,7 +221,7 @@ class GpgUnexpected(GpgBaseError): what: str -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgError(GpgBaseError): """This is a generic error status message, it might be followed by error location specific data.""" location: str @@ -235,30 +229,30 @@ class GpgError(GpgBaseError): more: str = "" -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgFailure(GpgBaseError): """This is the counterpart to SUCCESS and used to indicate a program failure.""" location: str code: int -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgBadArmor(GpgBaseError): """The ASCII armor is corrupted.""" -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgKeyExpired(GpgBaseError): """The key has expired.""" timestamp: int -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgKeyRevoked(GpgBaseError): """The used key has been revoked by its owner.""" -@frozen_dataclass +@dataclass(frozen=True, slots=True) class GpgNoSecKey(GpgBaseError): """The secret key is not available.""" keyid: str diff --git a/lib/ansible/parsing/yaml/constructor.py b/lib/ansible/parsing/yaml/constructor.py index 4f1cdfe2e6e..300dad38ca9 100644 --- a/lib/ansible/parsing/yaml/constructor.py +++ b/lib/ansible/parsing/yaml/constructor.py @@ -147,30 +147,32 @@ class AnsibleConstructor(SafeConstructor): AnsibleConstructor.add_constructor( u'tag:yaml.org,2002:map', - AnsibleConstructor.construct_yaml_map) + AnsibleConstructor.construct_yaml_map) # type: ignore[type-var] AnsibleConstructor.add_constructor( u'tag:yaml.org,2002:python/dict', - AnsibleConstructor.construct_yaml_map) + AnsibleConstructor.construct_yaml_map) # type: ignore[type-var] AnsibleConstructor.add_constructor( u'tag:yaml.org,2002:str', - AnsibleConstructor.construct_yaml_str) + AnsibleConstructor.construct_yaml_str) # type: ignore[type-var] AnsibleConstructor.add_constructor( u'tag:yaml.org,2002:python/unicode', - AnsibleConstructor.construct_yaml_str) + AnsibleConstructor.construct_yaml_str) # type: ignore[type-var] AnsibleConstructor.add_constructor( u'tag:yaml.org,2002:seq', - AnsibleConstructor.construct_yaml_seq) + AnsibleConstructor.construct_yaml_seq) # type: ignore[type-var] AnsibleConstructor.add_constructor( u'!unsafe', - AnsibleConstructor.construct_yaml_unsafe) + AnsibleConstructor.construct_yaml_unsafe) # type: ignore[type-var] AnsibleConstructor.add_constructor( u'!vault', - AnsibleConstructor.construct_vault_encrypted_unicode) + AnsibleConstructor.construct_vault_encrypted_unicode) # type: ignore[type-var] -AnsibleConstructor.add_constructor(u'!vault-encrypted', AnsibleConstructor.construct_vault_encrypted_unicode) +AnsibleConstructor.add_constructor( + u'!vault-encrypted', + AnsibleConstructor.construct_vault_encrypted_unicode) # type: ignore[type-var] diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index 107249def94..eb6301434af 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -2,15 +2,6 @@ lib/ansible/config/base.yml no-unwanted-files lib/ansible/executor/powershell/async_watchdog.ps1 pslint:PSCustomUseLiteralPath lib/ansible/executor/powershell/async_wrapper.ps1 pslint:PSCustomUseLiteralPath lib/ansible/executor/powershell/exec_wrapper.ps1 pslint:PSCustomUseLiteralPath -lib/ansible/galaxy/collection/__init__.py mypy-3.10:attr-defined # inline ignore has no effect -lib/ansible/galaxy/collection/__init__.py mypy-3.11:attr-defined # inline ignore has no effect -lib/ansible/galaxy/collection/__init__.py mypy-3.12:attr-defined # inline ignore has no effect -lib/ansible/galaxy/collection/gpg.py mypy-3.10:arg-type -lib/ansible/galaxy/collection/gpg.py mypy-3.11:arg-type -lib/ansible/galaxy/collection/gpg.py mypy-3.12:arg-type -lib/ansible/parsing/yaml/constructor.py mypy-3.10:type-var # too many occurrences to ignore inline -lib/ansible/parsing/yaml/constructor.py mypy-3.11:type-var # too many occurrences to ignore inline -lib/ansible/parsing/yaml/constructor.py mypy-3.12:type-var # too many occurrences to ignore inline lib/ansible/keyword_desc.yml no-unwanted-files lib/ansible/modules/apt.py validate-modules:parameter-invalid lib/ansible/modules/apt_repository.py validate-modules:parameter-invalid