Misc - #34
Merged
Merged
Conversation
Reviewer's GuideThis PR modernizes the scitrack module by adopting pathlib and type hints, standardizing type checks and error messages, hardening MD5 usage, refactoring the CachingLogger internals, updating the logging setup, revising tests to match the new API, and bumping Python support to 3.10 in CI configurations. Class diagram for updated CachingLogger and related functionsclassDiagram
class CachingLogger {
- _started: bool
- create_dir: bool
- _messages: list[str]
- _hostname: str
- _mode: str
- _log_file_path: str | None
- _logfile: Any
+ __init__(log_file_path: os.PathLike | None = None, create_dir: bool = True, mode: str = "w")
+ _reset(mode: str = "w")
+ log_file_path: str | None
+ log_file_path(path: str)
+ mode: str
+ mode(mode: str)
+ _record_file(file_class: str, file_path: str)
+ input_file(file_path: str, label: str = "input_file_path")
+ output_file(file_path: str, label: str = "output_file_path")
+ text_data(data: str, label: str | None = None)
+ log_message(msg: str, label: str | None = None)
+ log_args(args: dict | None = None)
+ shutdown()
+ log_versions(packages: list[str] | str | None = None)
}
class Path {
}
class logging {
}
CachingLogger --> Path : uses
CachingLogger --> logging : uses
class set_logger {
+ set_logger(log_file_path: str | os.PathLike, level: int = logging.DEBUG, mode: str = "w") : logging.Handler
}
class get_file_hexdigest {
+ get_file_hexdigest(filename: str | os.PathLike) : str
}
class get_text_hexdigest {
+ get_text_hexdigest(data: str | bytes) : str
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- In get_package_name, guard against inspect.getmodule returning None before accessing name to prevent unexpected attribute errors.
- In log_args, replace the
type(importlib)check with a clearer module‐type test (e.g. isinstance(x, types.ModuleType) or inspect.ismodule) for readability and correctness. - Ensure your GitHub Actions and nox matrices cover the same set of supported Python versions (e.g. re‐add 3.11/3.12 in testing_develop) so you’re consistently testing across all targets.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In get_package_name, guard against inspect.getmodule returning None before accessing __name__ to prevent unexpected attribute errors.
- In log_args, replace the `type(importlib)` check with a clearer module‐type test (e.g. isinstance(x, types.ModuleType) or inspect.ismodule) for readability and correctness.
- Ensure your GitHub Actions and nox matrices cover the same set of supported Python versions (e.g. re‐add 3.11/3.12 in testing_develop) so you’re consistently testing across all targets.
## Individual Comments
### Comment 1
<location> `src/scitrack/__init__.py:29` </location>
<code_context>
-def _create_path(path):
+def _create_path(path: str | os.PathLike) -> None:
"""creates path"""
- if os.path.exists(path):
</code_context>
<issue_to_address>
Potential confusion between file and directory creation in _create_path.
If a file path is provided, this function will create a directory with that file's name. Please clarify the expected input or rename the function to prevent incorrect usage.
Suggested implementation:
```python
def _create_dir(dir_path: str | os.PathLike) -> None:
"""creates a directory at the specified path"""
dir_path_obj: Path = Path(dir_path)
if dir_path_obj.exists():
return
dir_path_obj.mkdir(parents=True, exist_ok=True)
```
If `_create_path` is called elsewhere in the codebase, those references should be updated to `_create_dir` and ensure only directory paths are passed.
</issue_to_address>
### Comment 2
<location> `src/scitrack/__init__.py:68` </location>
<code_context>
break
- if type(vn) in (tuple, list):
+
+ if isinstance(vn, (tuple, list)):
vn = vn[0]
</code_context>
<issue_to_address>
Unpacking only the first element of version tuple/list may lose information.
Returning only the first element of a version tuple can misrepresent the actual version. Consider converting the entire tuple to a string for accurate reporting.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
if isinstance(vn, (tuple, list)):
vn = vn[0]
=======
if isinstance(vn, (tuple, list)):
vn = '.'.join(map(str, vn))
>>>>>>> REPLACE
</suggested_fix>
### Comment 3
<location> `src/scitrack/__init__.py:117` </location>
<code_context>
+ raise AttributeError(msg)
- path = abspath(path)
+ log_path: Path = Path(path).expanduser().resolve()
if self.create_dir:
- dirname = os.path.dirname(path)
</code_context>
<issue_to_address>
Using Path.resolve() may raise if the path does not exist.
Use 'resolve(strict=False)' to prevent exceptions when the path does not exist, especially if you intend to create new files.
</issue_to_address>
### Comment 4
<location> `src/scitrack/__init__.py:221` </location>
<code_context>
- packages[i] = p.__name__
+ to_check[i] = p.__name__
+
+ frame: types.FrameType | None = inspect.currentframe()
+ if frame is None:
+ return
</code_context>
<issue_to_address>
log_versions may leak frame references.
Assigning 'frame' and 'parent' can create reference cycles and hinder garbage collection. Use 'del frame, parent' after they are no longer needed.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Collaborator
Pull Request Test Coverage Report for Build 17084324771Details
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary by Sourcery
Refine core library with type annotations and pathlib usage, strengthen checksum functions, enhance logging behavior, and bump Python support to 3.10+ while updating CI and tests accordingly.
Enhancements:
Build:
CI:
Tests: