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

Refactor mypy_plugin.py for Improved Code Quality and Compliance #577

Closed
marcfreir opened this issue Nov 14, 2024 · 2 comments
Closed

Refactor mypy_plugin.py for Improved Code Quality and Compliance #577

marcfreir opened this issue Nov 14, 2024 · 2 comments

Comments

@marcfreir
Copy link

Hello, Markus Siemens & TinyDB team!

The TinyDB is a very interesting project. The project is well done. Upon reviewing the mypy_plugin.py file, it has a Pylint rating of 2.00/10, indicating multiple areas for improvement. This is just to be more formal with the evaluation. I made a few analyses, and just as a minor improvement, I could suggest, in the hook function inside get_dynamic_class_hook contains type-checking logic that could be error-prone and difficult to maintain. My suggestion is Refactor hook as a separate method, which can make the main logic clearer and easier to debug. For example:

     def _hook_logic(ctx: DynamicClassDefContext):
         klass = ctx.call.args[0]
         if not isinstance(klass, NameExpr):
             raise TypeError("Expected a NameExpr instance.")
         
         type_name = klass.fullname
         if type_name is None:
             raise ValueError("Type name cannot be None.")
         
         qualified = self.lookup_fully_qualified(type_name)
         if qualified is None:
             raise LookupError(f"Failed to find a fully qualified name for {type_name}")
         
         ctx.api.add_symbol_table_node(ctx.name, qualified)
     
     def get_dynamic_class_hook(self, fullname: str) -> CB[DynamicClassDef]:
         if fullname == 'tinydb.utils.with_typehint':
             return self._hook_logic
         return None

And thanks for the awesome project!

@msiemens
Copy link
Owner

msiemens commented Dec 3, 2024

Hey @marcfreir, I think that for the MyPy plugin, this is not a big concern. It is a small file (less than 40 lines of code) and does not need active maintenance when working on TinyDB itself. To me, there's not much to be gained by refactoring this file in particular.

@msiemens msiemens closed this as completed Dec 3, 2024
@marcfreir
Copy link
Author

Thanks for clarifying that perspective, @msiemens . You're right - given the file's small size and limited maintenance needs, the current implementation is sufficient for its purpose. I appreciate the feedback!

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

No branches or pull requests

2 participants