Added @staticmethod and type hints across code base #14
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduce type hints and static methods to the code, improving its readability, maintainability, and potentially performance. Let's break down the changes by file:
output_processor.py
:pregenerate_sm_output
andpregenerate_txt_output
functions, specifying the expected data types for thenote_data
parameter asdict[str, Any]
. The previous type hintdefaultdict(list)
was too restrictive and didn't accurately reflect the data structure.dict[str, Any]
is more flexible, while still providing some type information.@staticmethod
decorator is added to both functions. This indicates that the methods don't require access to the class's internal state (i.e.,self
). This change clarifies the intent and usage of these functions.input_processor.py
:output_processor.py
, type hints are added throughout, enhancing clarity. For example,convert_note
now specifies its input (line: str
) and return type (-> str
).parse_sm_input
also clearly defines its input and output types.@staticmethod
is applied to all methods in the class, emphasizing that these are utility functions operating independently of any specificInputProcessor
instance. The internal logic remains consistent while being made easier to follow and reuse due to type hints. The improved specificity from the older, more general type hints aids in understanding the data flow within these methods.parse_sm_input
is updated totuple[dict[str, Any], bool]
which means it returns a tuple including a dictionary containing the note data and a boolean variable.file_utils.py
:read_file
,write_file
,strip_filename
,collect_filenames
,getFilePaths
, andcheckFilePaths
. This makes the code much easier to understand and debug, especially when dealing with file paths and extensions. Notably, theextensions
parameter incollect_filenames
andgetFilePaths
are now typed asCollection[str]
, allowing for various iterable types to be passed in (e.g., lists, tuples, sets).cli_options.py
:filepath: str
) and return type (-> DataHandler
) of theread_SMtoData
andread_TXTtoData
functions. The methodswrite_DatatoSM
andwrite_DatatoTXT
also have the input type hints added.@staticmethod
is applied to all functions, as these utility functions are self-contained and don't need instance-specific context.data_processing/data_handler.py
:__init__
method now includes a type hint for thefilepath
parameter (filepath: str
).note_data
andprocessed_data
, usingdict[str, Any]
to represent the flexible structure of the data they hold.components/measure.py
:@staticmethod
indicating that they do not operate on instance data. Type hints are added to all function parameters and return values, clarifying expectations. For instance,calculate_timing
now explicitly states that it expects a list of strings or None values, and an integer, float, and float, and returns a list of strings. This strictness is vital when dealing with numerical computations and string formatting. The type hints forfind_gcd
,generate_measure
,fit_notes_to_measure
andplace_notes
also provides specific information about the input and output data types.In summary, these changes represent a significant improvement to the code's clarity and maintainability. By adding type hints and using static methods, the developers have made the code easier to understand, reason about, and refactor. This also facilitates better static analysis and error detection.