From a796a68e02521a1db2ee309c021671a904fe14df Mon Sep 17 00:00:00 2001 From: J08nY Date: Sun, 5 Feb 2023 16:25:43 +0100 Subject: Fix mypy issues. --- .pre-commit-config.yaml | 10 ++++++---- Makefile | 4 ++-- pyecsca/ec/model.py | 6 ++---- pyecsca/ec/mult.py | 18 +++++++++--------- pyecsca/ec/signature.py | 6 +++--- pyecsca/misc/cfg.py | 5 ++++- pyecsca/sca/scope/chipwhisperer.py | 5 +++-- pyecsca/sca/target/binary.py | 6 ++++-- pyecsca/sca/trace/align.py | 2 +- pyecsca/sca/trace/edit.py | 4 ++-- pyecsca/sca/trace/trace.py | 8 ++++---- 11 files changed, 40 insertions(+), 34 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2ac2274..3804976 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.4.0 + rev: v4.4.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -11,12 +11,14 @@ repos: - id: check-yaml - id: check-added-large-files - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.812 + rev: v0.991 hooks: - id: mypy - args: [--ignore-missing-imports, --show-error-codes] + additional_dependencies: + - "types-setuptools" + args: [--ignore-missing-imports, --show-error-codes, --namespace-packages] - repo: https://github.com/PyCQA/flake8 - rev: 3.9.0 + rev: 6.0.0 hooks: - id: flake8 args: ["--extend-ignore=E501,F405,F403,F401,E126,E203"] diff --git a/Makefile b/Makefile index b734bd4..547797d 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,10 @@ test-all: nose2 -s test -C -v ${TESTS} typecheck: - mypy --namespace-packages -p pyecsca --ignore-missing-imports --show-error-codes + mypy --namespace-packages -p pyecsca --ignore-missing-imports --show-error-codes --check-untyped-defs typecheck-all: - mypy --namespace-packages -p pyecsca -p test --ignore-missing-imports --show-error-codes + mypy --namespace-packages -p pyecsca -p test --ignore-missing-imports --show-error-codes --check-untyped-defs codestyle: flake8 --extend-ignore=E501,F405,F403,F401,E126,E203 pyecsca diff --git a/pyecsca/ec/model.py b/pyecsca/ec/model.py index c6305a6..e714991 100644 --- a/pyecsca/ec/model.py +++ b/pyecsca/ec/model.py @@ -63,9 +63,8 @@ class EFDCurveModel(CurveModel): return parse(line.replace("^", "**"), mode=mode) with resource_stream(__name__, file_path) as f: - line = f.readline() - while line: - line = line.decode("ascii").rstrip() + for raw in f.readlines(): + line = raw.decode("ascii").rstrip() if line.startswith("name"): cls.name = line[5:] elif line.startswith("parameter"): @@ -90,7 +89,6 @@ class EFDCurveModel(CurveModel): cls.from_weierstrass.append(format_eq(line[16:])) else: cls.full_weierstrass.append(format_eq(line)) - line = f.readline() def __read_coordinate_dir(self, cls, dir_path, name): cls.coordinates[name] = EFDCoordinateModel(dir_path, name, self) diff --git a/pyecsca/ec/mult.py b/pyecsca/ec/mult.py index 47c36b1..dc89237 100644 --- a/pyecsca/ec/mult.py +++ b/pyecsca/ec/mult.py @@ -203,7 +203,7 @@ class LTRMultiplier(ScalarMultiplier): self, add: AdditionFormula, dbl: DoublingFormula, - scl: ScalingFormula = None, + scl: Optional[ScalingFormula] = None, always: bool = False, complete: bool = True, short_circuit: bool = True, @@ -254,7 +254,7 @@ class RTLMultiplier(ScalarMultiplier): self, add: AdditionFormula, dbl: DoublingFormula, - scl: ScalingFormula = None, + scl: Optional[ScalingFormula] = None, always: bool = False, short_circuit: bool = True, ): @@ -300,7 +300,7 @@ class CoronMultiplier(ScalarMultiplier): self, add: AdditionFormula, dbl: DoublingFormula, - scl: ScalingFormula = None, + scl: Optional[ScalingFormula] = None, short_circuit: bool = True, ): super().__init__(short_circuit=short_circuit, add=add, dbl=dbl, scl=scl) @@ -334,8 +334,8 @@ class LadderMultiplier(ScalarMultiplier): def __init__( self, ladd: LadderFormula, - dbl: DoublingFormula = None, - scl: ScalingFormula = None, + dbl: Optional[DoublingFormula] = None, + scl: Optional[ScalingFormula] = None, complete: bool = True, short_circuit: bool = True, ): @@ -381,7 +381,7 @@ class SimpleLadderMultiplier(ScalarMultiplier): self, add: AdditionFormula, dbl: DoublingFormula, - scl: ScalingFormula = None, + scl: Optional[ScalingFormula] = None, complete: bool = True, short_circuit: bool = True, ): @@ -424,7 +424,7 @@ class DifferentialLadderMultiplier(ScalarMultiplier): self, dadd: DifferentialAdditionFormula, dbl: DoublingFormula, - scl: ScalingFormula = None, + scl: Optional[ScalingFormula] = None, complete: bool = True, short_circuit: bool = True, ): @@ -469,7 +469,7 @@ class BinaryNAFMultiplier(ScalarMultiplier): add: AdditionFormula, dbl: DoublingFormula, neg: NegationFormula, - scl: ScalingFormula = None, + scl: Optional[ScalingFormula] = None, short_circuit: bool = True, ): super().__init__( @@ -517,7 +517,7 @@ class WindowNAFMultiplier(ScalarMultiplier): dbl: DoublingFormula, neg: NegationFormula, width: int, - scl: ScalingFormula = None, + scl: Optional[ScalingFormula] = None, precompute_negation: bool = False, short_circuit: bool = True, ): diff --git a/pyecsca/ec/signature.py b/pyecsca/ec/signature.py index 972af19..d9b49f7 100644 --- a/pyecsca/ec/signature.py +++ b/pyecsca/ec/signature.py @@ -64,7 +64,7 @@ class ECDSAAction(Action): self.msg = msg def __repr__(self): - return f"{self.__class__.__name__}({self.params}, {self.hash_algo}, {self.msg})" + return f"{self.__class__.__name__}({self.params}, {self.hash_algo}, {self.msg!r})" @public @@ -84,7 +84,7 @@ class ECDSASignAction(ECDSAAction): self.privkey = privkey def __repr__(self): - return f"{self.__class__.__name__}({self.params}, {self.hash_algo}, {self.msg}, {self.privkey})" + return f"{self.__class__.__name__}({self.params}, {self.hash_algo}, {self.msg!r}, {self.privkey})" @public @@ -107,7 +107,7 @@ class ECDSAVerifyAction(ECDSAAction): self.pubkey = pubkey def __repr__(self): - return f"{self.__class__.__name__}({self.params}, {self.hash_algo}, {self.msg}, {self.signature}, {self.pubkey})" + return f"{self.__class__.__name__}({self.params}, {self.hash_algo}, {self.msg!r}, {self.signature}, {self.pubkey})" @public diff --git a/pyecsca/misc/cfg.py b/pyecsca/misc/cfg.py index 24b83f8..54579d9 100644 --- a/pyecsca/misc/cfg.py +++ b/pyecsca/misc/cfg.py @@ -5,6 +5,7 @@ This includes how errors are handled, or which :py:class:`~pyecsca.ec.mod.Mod` i """ from copy import deepcopy from contextvars import ContextVar, Token +from typing import Optional from public import public @@ -178,6 +179,7 @@ class TemporaryConfig: cfg.some_property = some_value ... """ + token: Optional[Token] def __init__(self): self.token = None @@ -188,4 +190,5 @@ class TemporaryConfig: return self.new_config def __exit__(self, t, v, tb): - resetconfig(self.token) + if self.token: + resetconfig(self.token) diff --git a/pyecsca/sca/scope/chipwhisperer.py b/pyecsca/sca/scope/chipwhisperer.py index f1c8d94..5df98dd 100644 --- a/pyecsca/sca/scope/chipwhisperer.py +++ b/pyecsca/sca/scope/chipwhisperer.py @@ -31,8 +31,9 @@ class ChipWhispererScope(Scope): # pragma: no cover if pretrig != 0: raise ValueError("ChipWhisperer does not support pretrig samples.") self.scope.clock.clkgen_freq = frequency - self.scope.samples = posttrig - return self.scope.clock.freq_ctr, self.scope.samples + self.scope.adc.samples = posttrig + # TODO: Fix this, broken with new CW api. + return self.scope.clock.freq_ctr, self.scope.adc.samples def setup_channel( self, channel: str, coupling: str, range: float, offset: float, enable: bool diff --git a/pyecsca/sca/target/binary.py b/pyecsca/sca/target/binary.py index 291f8bc..9e782f6 100644 --- a/pyecsca/sca/target/binary.py +++ b/pyecsca/sca/target/binary.py @@ -62,7 +62,9 @@ class BinaryTarget(SerialTarget): def disconnect(self): if self.process is None: return - self.process.stdin.close() - self.process.stdout.close() + if self.process.stdin is not None: + self.process.stdin.close() + if self.process.stdout is not None: + self.process.stdout.close() self.process.terminate() self.process.wait() diff --git a/pyecsca/sca/trace/align.py b/pyecsca/sca/trace/align.py index ac30fe5..598a62e 100644 --- a/pyecsca/sca/trace/align.py +++ b/pyecsca/sca/trace/align.py @@ -157,7 +157,7 @@ def align_offset( def align_func(trace): length = len(trace.samples) - best_distance = 0 + best_distance = 0.0 best_offset = 0 for offset in range(-max_offset, max_offset): start = reference_offset + offset diff --git a/pyecsca/sca/trace/edit.py b/pyecsca/sca/trace/edit.py index 83653d0..707c1a3 100644 --- a/pyecsca/sca/trace/edit.py +++ b/pyecsca/sca/trace/edit.py @@ -1,13 +1,13 @@ """This module provides functions for editing traces as if they were tapes you can trim, reverse, etc.""" import numpy as np from public import public -from typing import Union, Tuple, Any +from typing import Union, Tuple, Any, Optional from .trace import Trace @public -def trim(trace: Trace, start: int = None, end: int = None) -> Trace: +def trim(trace: Trace, start: Optional[int] = None, end: Optional[int] = None) -> Trace: """ Trim the `trace` samples, output contains samples between the `start` and `end` indices. diff --git a/pyecsca/sca/trace/trace.py b/pyecsca/sca/trace/trace.py index 4ddad23..0d5d027 100644 --- a/pyecsca/sca/trace/trace.py +++ b/pyecsca/sca/trace/trace.py @@ -1,6 +1,6 @@ """This module provides the Trace class.""" import weakref -from typing import Any, Mapping, Sequence +from typing import Any, Mapping, Sequence, Optional from copy import copy, deepcopy from numpy import ndarray @@ -16,7 +16,7 @@ class Trace: samples: ndarray def __init__( - self, samples: ndarray, meta: Mapping[str, Any] = None, trace_set: Any = None + self, samples: ndarray, meta: Optional[Mapping[str, Any]] = None, trace_set: Any = None ): """ Construct a new trace. @@ -106,9 +106,9 @@ class CombinedTrace(Trace): def __init__( self, samples: ndarray, - meta: Mapping[str, Any] = None, + meta: Optional[Mapping[str, Any]] = None, trace_set: Any = None, - parents: Sequence[Trace] = None, + parents: Optional[Sequence[Trace]] = None, ): super().__init__(samples, meta, trace_set=trace_set) self.parents = None -- cgit v1.2.3-70-g09d2