Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused code, rework type hints #730

Closed
wants to merge 3 commits into from
Closed

Conversation

NiklasHargarter
Copy link
Contributor

@NiklasHargarter NiklasHargarter commented Jul 16, 2024

What

Changes type hints used in core modules. Functionality is unchanged.
In limited cases, changes have been made to things other than type hints so that a type hint is correct.

Iterable is replaced by list. This is because only lists are ever used and passed to methods that are typed with iterable. Iterable allows more abstraction that is never used and unnecessarily hides what is actually being passed. So I think using list makes the code cleaner.

The use of a non-optional type with a default value of None, e.g. name: str = None, is either changed to Optional[type] or the default of None is removed. This depends on whether None is actually a valid value for that variable/argument, and whether a default value makes sense.

Imports from typing that are deprecated in 3.9, such as List, are replaced with what typing recommends to use.

Removed plugin.description as I could not find a use for it anywhere and no plugin overwrites it.

Unclear if can be removed

Troubadix.main method default args
exists so that Troubadix main can be called internally or probably imported as a module and not only run exclusively from the command line.

Why

So that the typing hint system is correct and makes future development easier.

References

Checklist

  • Tests

Copy link

github-actions bot commented Jul 16, 2024

Conventional Commits Report

Type Number
Changed 3

🚀 Conventional commits found.

@NiklasHargarter NiklasHargarter changed the title Change: argparse check_cpu_count change logic and add warnings Rework type hints Jul 16, 2024
Copy link

codecov bot commented Jul 16, 2024

Codecov Report

Attention: Patch coverage is 85.71429% with 7 lines in your changes missing coverage. Please review.

Project coverage is 79.56%. Comparing base (2159f78) to head (09baab9).
Report is 6 commits behind head on main.

Files Patch % Lines
troubadix/plugin.py 62.50% 3 Missing ⚠️
troubadix/argparser.py 86.66% 2 Missing ⚠️
troubadix/troubadix.py 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #730      +/-   ##
==========================================
+ Coverage   79.51%   79.56%   +0.04%     
==========================================
  Files          86       86              
  Lines        2910     2917       +7     
  Branches      611      611              
==========================================
+ Hits         2314     2321       +7     
- Misses        450      451       +1     
+ Partials      146      145       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@NiklasHargarter NiklasHargarter changed the title Rework type hints Remove unused code, rework type hints Jul 18, 2024
@@ -18,13 +18,17 @@
""" Argument parser for troubadix """

import sys
from argparse import ArgumentParser, Namespace
import warnings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the pontos.Terminal with warn or warning

Comment on lines +47 to 76
def check_cpu_count(number_str: str) -> int:
"""
Ensures the given value is a valid CPU core count.

Args:
number_str (str): The number of CPU cores as a string.

Returns:
int: A valid CPU core count.
"""
try:
number = int(number_str)
except ValueError as e:
raise ArgumentTypeError(
f"Invalid number of jobs: '{number_str}' is not an integer."
) from e
if number > MAX_JOB_COUNT:
warnings.warn(
f"Requested number of jobs ({number}) "
"exceeds available CPU cores ({MAX_JOB_COUNT}). "
f"Using maximum available: {MAX_JOB_COUNT}.",
)
return MAX_JOB_COUNT
if number < 1:
return max_count // 2
warnings.warn(
f"Requested number of jobs ({number}) is less than 1. "
f"Using default: {DEFAULT_JOB_COUNT}.",
)
return DEFAULT_JOB_COUNT
return number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh I prefer the previous version more.

Moving the MAX_JOB_COUNT and DEFAULT_JOB_COUNT variables out is OK, but the rest seems a bit over engineered to me

@@ -216,11 +238,11 @@ def parse_args(
"-j",
"--n-jobs",
dest="n_jobs",
default=max(1, cpu_count() // 2),
default=str(DEFAULT_JOB_COUNT),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default=str(DEFAULT_JOB_COUNT),
default=DEFAULT_JOB_COUNT,

int(<int as input>) just returns that int, so this should be cool / less head scratchy why the default it str

type=check_cpu_count,
help=(
"Define number of jobs, that should run simultaneously. "
"Default: %(default)s"
f"Default: {DEFAULT_JOB_COUNT} (half of the available CPU cores)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f"Default: {DEFAULT_JOB_COUNT} (half of the available CPU cores)"
f"Default: {DEFAULT_JOB_COUNT} (half of the available CPU cores)"

Please revert this, as the previous implementation will always point to the default value of this argument

return number


def parse_args(
terminal: Terminal,
args: Iterable[str] = None,
args: Sequence[str],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is Sequence better here than Iterable ?


@abstractmethod
def run(self) -> Iterator[LinterResult]:
pass

def fix(self) -> Iterator[LinterResult]:
return []
return iter([])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make anything better / fixes a bug somewhere ?

self.root = root
self.nasl_files = nasl_files


class Plugin(ABC):
"""A linter plugin"""

name: str = None
description: str = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did this field go ?

@NiklasHargarter NiklasHargarter deleted the rework-type-hints branch August 8, 2024 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants