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

Open
marcfreir opened this issue Nov 14, 2024 · 0 comments
Open

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

marcfreir opened this issue Nov 14, 2024 · 0 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!

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

1 participant