From a307cba5f3155585ee6f29dd199ac915ecd4b943 Mon Sep 17 00:00:00 2001 From: Pavlos Kallis Date: Tue, 5 May 2020 13:25:23 +0300 Subject: [PATCH 1/3] Introduce static types and remove unneeded arguments --- src/pdmongo/core.py | 53 ++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/pdmongo/core.py b/src/pdmongo/core.py index bfc91b8..8fa0738 100644 --- a/src/pdmongo/core.py +++ b/src/pdmongo/core.py @@ -1,9 +1,19 @@ +from typing import Any +from typing import Dict +from typing import Iterator +from typing import List +from typing import Optional +from typing import Sequence +from typing import Union + from pandas import DataFrame from pymongo import MongoClient +from pymongo.database import Database +from pymongo.results import InsertManyResult from pymongo.uri_parser import parse_uri -def _get_db_instance(db): +def _get_db_instance(db: Union[str, Database]) -> MongoClient: """ Retrieve the pymongo.database.Database instance. @@ -26,7 +36,7 @@ def _get_db_instance(db): return db -def _handle_exists_collection(name, exists, db): +def _handle_exists_collection(name: str, exists: Optional[str], db: Database) -> None: """ Handles the `if_exists` argument of `to_mongo`. @@ -55,7 +65,7 @@ def _handle_exists_collection(name, exists, db): raise ValueError(f"'{exists}' is not valid for if_exists") -def _split_in_chunks(lst, chunksize): +def _split_in_chunks(lst: Sequence[Any], chunksize: int) -> Iterator[Sequence[Any]]: """ Splits a list in chunks based on provided chunk size. @@ -73,7 +83,7 @@ def _split_in_chunks(lst, chunksize): yield lst[i:i + chunksize] -def _validate_chunksize(chunksize): +def _validate_chunksize(chunksize: int) -> None: """ Raises the proper exception if chunksize is not valid. @@ -89,14 +99,13 @@ def _validate_chunksize(chunksize): def read_mongo( - collection, - query, - db, - index_col=None, - extra=None, - columns=None, - chunksize=None -): + collection: str, + query: List[Dict[str, Any]], + db: Union[str, Database], + index_col: Optional[Union[str, List[str]]] = None, + extra: Optional[Dict[str, Any]] = None, + chunksize: Optional[int] = None +) -> DataFrame: """ Read MongoDB query into a DataFrame. @@ -115,8 +124,8 @@ def read_mongo( The database to use index_col : str or list of str, optional, default: None Column(s) to set as index(MultiIndex). - extra : list, tuple or dict, optional, default: None - List of parameters to pass to find/aggregate method. + extra : dict, optional, default: None + List of parameters to pass to aggregate method. chunksize : int, default None If specified, return an iterator where `chunksize` is the number of docs to include in each chunk. @@ -143,14 +152,14 @@ def read_mongo( def to_mongo( - frame, - name, - db, - if_exists="fail", - index=True, - index_label=None, - chunksize=None, -): + frame: DataFrame, + name: str, + db: Union[str, Database], + if_exists: Optional[str] = "fail", + index: Optional[bool] = True, + index_label: Optional[Union[str, Sequence[str]]] = None, + chunksize: Optional[int] = None, +) -> Union[List[InsertManyResult], InsertManyResult]: """ Write records stored in a DataFrame to a MongoDB collection. From ce4e47c9fbfd721d733df493af47a14a6850e40b Mon Sep 17 00:00:00 2001 From: Pavlos Kallis Date: Tue, 5 May 2020 22:39:52 +0300 Subject: [PATCH 2/3] Add mypy to CI --- setup.cfg | 6 ++++++ tox.ini | 2 ++ 2 files changed, 8 insertions(+) diff --git a/setup.cfg b/setup.cfg index a0bfcb2..c01ff55 100644 --- a/setup.cfg +++ b/setup.cfg @@ -59,3 +59,9 @@ default_section = THIRDPARTY forced_separate = test_pdmongo not_skip = __init__.py skip = migrations + + +[mypy] +python_version = 3.6 +strict = True +ignore_missing_imports = True \ No newline at end of file diff --git a/tox.ini b/tox.ini index 0eed220..4814984 100644 --- a/tox.ini +++ b/tox.ini @@ -46,12 +46,14 @@ deps = readme-renderer pygments isort + mypy skip_install = true commands = python setup.py check --strict --metadata --restructuredtext check-manifest {toxinidir} flake8 src tests setup.py isort --verbose --check-only --diff --recursive src tests setup.py + mypy src [testenv:docs] usedevelop = true From 82a8ab542621d862fbcc81c2e78dde8e6b6aa9de Mon Sep 17 00:00:00 2001 From: Pavlos Kallis Date: Tue, 5 May 2020 22:19:25 +0300 Subject: [PATCH 3/3] =?UTF-8?q?Bump=20version:=200.0.2=20=E2=86=92=200.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- CHANGELOG.rst | 6 ++++++ README.rst | 4 ++-- docs/conf.py | 2 +- setup.py | 2 +- src/pdmongo/__init__.py | 2 +- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 36db1d1..4283257 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.0.2 +current_version = 0.1.0 commit = True tag = True diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7eee770..68c4b0e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,12 @@ Changelog ========= +0.1.0 (2020-05-05) +------------------ +* Added static typing +* Added mypy to travis CI +* Removed unecessary params + 0.0.2 (2020-05-04) ------------------ diff --git a/README.rst b/README.rst index 54f8232..3aedb66 100644 --- a/README.rst +++ b/README.rst @@ -47,9 +47,9 @@ Overview :alt: Supported implementations :target: https://pypi.org/project/pdmongo -.. |commits-since| image:: https://img.shields.io/github/commits-since/pakallis/python-pandas-mongo/v0.0.2.svg +.. |commits-since| image:: https://img.shields.io/github/commits-since/pakallis/python-pandas-mongo/v0.1.0.svg :alt: Commits since latest release - :target: https://github.com/pakallis/python-pandas-mongo/compare/v0.0.2...master + :target: https://github.com/pakallis/python-pandas-mongo/compare/v0.1.0...master diff --git a/docs/conf.py b/docs/conf.py index 19901c6..1f09fdf 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,7 +20,7 @@ year = '2020' author = 'Pavlos Kallis' copyright = '{0}, {1}'.format(year, author) -version = release = '0.0.2' +version = release = '0.1.0' pygments_style = 'trac' templates_path = ['.'] diff --git a/setup.py b/setup.py index 5e83d7a..969eac2 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ def read(*names, **kwargs): setup( name='pdmongo', - version='0.0.2', + version='0.1.0', license='MIT', description='Transfer data between pandas dataframes and MongoDB', long_description='%s\n%s' % ( diff --git a/src/pdmongo/__init__.py b/src/pdmongo/__init__.py index 65bc4f4..d82858c 100644 --- a/src/pdmongo/__init__.py +++ b/src/pdmongo/__init__.py @@ -6,4 +6,4 @@ pandas.DataFrame.to_mongo = to_mongo __all__ = ['read_mongo', 'to_mongo'] -__version__ = '0.0.2' +__version__ = '0.1.0'