From 3084aa505554e1c00021e502ebe558747ae74b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sta=C5=9Bczak?= Date: Thu, 6 Jun 2024 00:00:42 +0200 Subject: [PATCH 1/2] Remove unused code Detected using [vulture]: ``` $ vulture . --exclude __pypackages__,.venv onetimepass/db/models.py:67: unused variable 'OTPParams' (60% confidence) onetimepass/db/models.py:81: unused function 'valid_base32_secret' (60% confidence) onetimepass/db/models.py:89: unused function 'valid_params_for_otp_type' (60% confidence) onetimepass/db/models.py:106: unused function 'supported_version' (60% confidence) onetimepass/db/models.py:119: unused method 'add_totp_alias' (60% confidence) onetimepass/db/models.py:159: unused function 'get_params_by_type' (60% confidence) onetimepass/enum.py:12: unused method '_missing_' (60% confidence) onetimepass/enum.py:27: unused variable 'SHA256' (60% confidence) onetimepass/enum.py:28: unused variable 'SHA512' (60% confidence) onetimepass/otp.py:146: unused function 'show' (60% confidence) onetimepass/otp.py:220: unused function 'init' (60% confidence) onetimepass/otp.py:261: unused function 'delete' (60% confidence) onetimepass/otp.py:281: unused function 'list_' (60% confidence) onetimepass/otp.py:302: unused function 'export' (60% confidence) onetimepass/otp.py:316: unused function 'import_' (60% confidence) onetimepass/otp.py:351: unused function 'add_uri' (60% confidence) onetimepass/otp.py:427: unused function 'add_hotp' (60% confidence) onetimepass/otp.py:479: unused function 'add_totp' (60% confidence) onetimepass/otp.py:539: unused function 'rename' (60% confidence) onetimepass/otpauth/errors.py:10: unused variable 'msg_template' (60% confidence) onetimepass/otpauth/errors.py:18: unused variable 'msg_template' (60% confidence) onetimepass/otpauth/errors.py:23: unused variable 'msg_template' (60% confidence) onetimepass/otpauth/schemas.py:24: unused function 'issuer_must_match_pattern' (60% confidence) onetimepass/otpauth/schemas.py:109: unused function 'strip_leading_spaces' (60% confidence) onetimepass/otpauth/schemas.py:125: unused function 'scheme_must_be_otpauth' (60% confidence) onetimepass/otpauth/schemas.py:131: unused function 'set_label' (60% confidence) onetimepass/otpauth/schemas.py:135: unused function 'parameters_issuer_equals_label_issuer' (60% confidence) onetimepass/settings.py:12: unused variable 'DEFAULT_HASH_ALGORITHM' (60% confidence) ``` Some of those were false positives: most notably, `@validator`-decorated functions in Pydantic models and `@click.*`-decorated functions that implement the bottom-most commands. [vulture]: https://github.com/jendrikseipp/vulture/tree/v2.11 --- onetimepass/db/models.py | 32 -------------------------------- onetimepass/settings.py | 1 - 2 files changed, 33 deletions(-) diff --git a/onetimepass/db/models.py b/onetimepass/db/models.py index c293a11..b6a3338 100644 --- a/onetimepass/db/models.py +++ b/onetimepass/db/models.py @@ -64,9 +64,6 @@ class TOTPParams(BaseModel): time_step_seconds: int -OTPParams = typing.Union[HOTPParams, TOTPParams] - - class AliasSchema(BaseModel): secret: str digits_count: int @@ -116,29 +113,6 @@ def initialize(cls) -> DatabaseSchema: def add_alias(self, name: str, data: AliasSchema): self.otp[name] = data - def add_totp_alias( - self, - name: str, - label: str, - issuer: str, - secret: str, - digits_count: int, - hash_algorithm: HashAlgorithm, - initial_time: datetime.datetime, - time_step_seconds: int = settings.DEFAULT_TIME_STEP_SECONDS, - ): - self.otp[name] = AliasSchema( - secret=secret, - label=label, - issuer=issuer, - digits_count=digits_count, - hash_algorithm=hash_algorithm, - otp_type=OTPType.TOTP, - params=TOTPParams( - initial_time=initial_time, time_step_seconds=time_step_seconds - ), - ) - def merge(self, other: DatabaseSchema): if other.version != self.version: raise exceptions.DBUnsupportedMigration( @@ -154,9 +128,3 @@ def merge(self, other: DatabaseSchema): ) self.otp |= other.otp - - -def get_params_by_type( - type_: OTPType, -) -> typing.Type[HOTPParams] | typing.Type[TOTPParams]: - return {OTPType.HOTP: HOTPParams, OTPType.TOTP: TOTPParams}[type_] diff --git a/onetimepass/settings.py b/onetimepass/settings.py index 26e5bfa..87f9291 100644 --- a/onetimepass/settings.py +++ b/onetimepass/settings.py @@ -9,7 +9,6 @@ SUPPORTED_DB_VERSION = [ DEFAULT_DB_VERSION, ] -DEFAULT_HASH_ALGORITHM = "sha1" DEFAULT_DIGITS_COUNT = 6 DEFAULT_TIME_STEP_SECONDS = 30 DEFAULT_INITIAL_HOTP_COUNTER = 0 From 0c71bb8e23f8379428a542e9aabf1cd816e762e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sta=C5=9Bczak?= Date: Thu, 6 Jun 2024 00:26:09 +0200 Subject: [PATCH 2/2] Add `vulture` to the pre-commit hooks The detector of an unused code. See: https://github.com/jendrikseipp/vulture/tree/v2.11 --- .pre-commit-config.yaml | 4 ++++ pyproject.toml | 4 ++++ whitelist.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 whitelist.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 073d31f..ea9ad4a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,3 +24,7 @@ repos: rev: v3.13.0 hooks: - id: reorder-python-imports +- repo: https://github.com/jendrikseipp/vulture + rev: 'v2.11' + hooks: + - id: vulture diff --git a/pyproject.toml b/pyproject.toml index 697f8ef..b738ba4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,5 +35,9 @@ dev = [ "pre-commit~=2.16", ] +[tool.vulture] +exclude = ["__pypackages__", ".venv"] +paths = ["."] + [project.scripts] otp = "onetimepass.otp:otp" diff --git a/whitelist.py b/whitelist.py new file mode 100644 index 0000000..0b9b01a --- /dev/null +++ b/whitelist.py @@ -0,0 +1,35 @@ +"""A whitelist for `vulture` (dead code detector) + +Generated via +``` +$ vulture --make-whitelist >whitelist.py +``` + +See: +https://github.com/jendrikseipp/vulture/tree/v2.11?tab=readme-ov-file#whitelists +""" + +valid_base32_secret # unused function (onetimepass/db/models.py:80) +valid_params_for_otp_type # unused function (onetimepass/db/models.py:88) +supported_version # unused function (onetimepass/db/models.py:105) +_._missing_ # unused method (onetimepass/enum.py:12) +SHA256 # unused variable (onetimepass/enum.py:27) +SHA512 # unused variable (onetimepass/enum.py:28) +show # unused function (onetimepass/otp.py:146) +init # unused function (onetimepass/otp.py:220) +delete # unused function (onetimepass/otp.py:261) +list_ # unused function (onetimepass/otp.py:281) +export # unused function (onetimepass/otp.py:302) +import_ # unused function (onetimepass/otp.py:316) +add_uri # unused function (onetimepass/otp.py:351) +add_hotp # unused function (onetimepass/otp.py:427) +add_totp # unused function (onetimepass/otp.py:479) +rename # unused function (onetimepass/otp.py:539) +msg_template # unused variable (onetimepass/otpauth/errors.py:10) +msg_template # unused variable (onetimepass/otpauth/errors.py:18) +msg_template # unused variable (onetimepass/otpauth/errors.py:23) +issuer_must_match_pattern # unused function (onetimepass/otpauth/schemas.py:24) +strip_leading_spaces # unused function (onetimepass/otpauth/schemas.py:109) +scheme_must_be_otpauth # unused function (onetimepass/otpauth/schemas.py:125) +set_label # unused function (onetimepass/otpauth/schemas.py:131) +parameters_issuer_equals_label_issuer # unused function (onetimepass/otpauth/schemas.py:135)