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

Code Quality Improvements Based on Pylint Report #576

Open
marcfreir opened this issue Nov 14, 2024 · 1 comment
Open

Code Quality Improvements Based on Pylint Report #576

marcfreir opened this issue Nov 14, 2024 · 1 comment

Comments

@marcfreir
Copy link

marcfreir commented Nov 14, 2024

Hello, Markus Siemens & TinyDB team!

I ran a Pylint analysis on the TinyDB codebase, and I identified some areas where code quality could be improved. Below is a summary of the findings, of the code block for the function def __repr__(self), along with suggestions for addressing each category. These changes should enhance code readability, maintainability, and adherence to Python's best practices.

Enhanced f-String Usage:

  • Issue: The __repr__ method uses .format(), which can be less readable than f-strings.
  • Suggestion: Replace .format() with f-strings. For example:
    # Current:
    args = [
        'tables={}'.format(list(self.tables())),
        'tables_count={}'.format(len(self.tables())),
        'default_table_documents_count={}'.format(self.__len__()),
        'all_tables_documents_count={}'.format(
            ['{}={}'.format(table, len(self.table(table))) for table in self.tables()]
        ),
    ]
    # Suggested:
    args = [
        f'tables={list(self.tables())}',
        f'tables_count={len(self.tables())}',
        f'default_table_documents_count={self.__len__()}',
        f'all_tables_documents_count={[f"{table}={len(self.table(table))}" for table in self.tables()]}',
    ]

And thanks for the awesome project!

@marcfreir
Copy link
Author

marcfreir commented Nov 16, 2024

I have also implemented a test file for database.py, and after I run the test with Pytest, I got the following report:

vscode@e7bce794d2b3:/workspaces/tinydb/tests$ pytest test_database.py -v
============================================================================= test session starts =============================================================================
platform linux -- Python 3.10.6, pytest-8.2.2, pluggy-1.5.0 -- /usr/bin/python
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/workspaces/tinydb/tests/.hypothesis/examples')
rootdir: /workspaces/tinydb
configfile: pytest.ini
plugins: cov-6.0.0, rerunfailures-11.1.2, xdoctest-1.0.2, shard-0.1.2, hypothesis-5.35.1, xdist-3.3.1
collected 7 items                                                                                                                                                             
Running 7 items in this shard: tests/test_database.py::test_table_creation, tests/test_database.py::test_insert_and_read, tests/test_database.py::test_drop_table, tests/test_database.py::test_drop_all_tables, tests/test_database.py::test_len_and_iteration, tests/test_database.py::test_context_manager, tests/test_database.py::test_repr

test_database.py::test_table_creation PASSED                                                                                                                            [ 14%]
test_database.py::test_insert_and_read PASSED                                                                                                                           [ 28%]
test_database.py::test_drop_table PASSED                                                                                                                                [ 42%]
test_database.py::test_drop_all_tables PASSED                                                                                                                           [ 57%]
test_database.py::test_len_and_iteration PASSED                                                                                                                         [ 71%]
test_database.py::test_context_manager PASSED                                                                                                                           [ 85%]
test_database.py::test_repr PASSED                                                                                                                                      [100%]

---------- coverage: platform linux, python 3.10.6-final-0 -----------
Name                                       Stmts   Miss  Cover
--------------------------------------------------------------
/workspaces/tinydb/tinydb/__init__.py          5      0   100%
/workspaces/tinydb/tinydb/database.py         56      2    96%
/workspaces/tinydb/tinydb/middlewares.py      33      0   100%
/workspaces/tinydb/tinydb/mypy_plugin.py      25     25     0%
/workspaces/tinydb/tinydb/operations.py       24     24     0%
/workspaces/tinydb/tinydb/queries.py         128     86    33%
/workspaces/tinydb/tinydb/storages.py         60      8    87%
/workspaces/tinydb/tinydb/table.py           219    128    42%
/workspaces/tinydb/tinydb/utils.py            73     34    53%
/workspaces/tinydb/tinydb/version.py           1      0   100%
--------------------------------------------------------------
TOTAL                                        624    307    51%


============================================================================== 7 passed in 0.10s ==============================================================================

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