davidlatwe / montydb

Monty, Mongo tinified. MongoDB implemented in Python !
BSD 3-Clause "New" or "Revised" License
579 stars 29 forks source link

Python 3 type hints #38

Open cclauss opened 3 years ago

cclauss commented 3 years ago

Python type hints would be an interesting direction once Python 2 support is dropped...

$ mypy --ignore-missing-imports .

montydb/montydb/types/_bson.py:279: error: Need type annotation for 'custom_json_hooks' (hint: "custom_json_hooks: Dict[<type>, <type>] = ...")
montydb/montydb/storage/memory.py:14: error: Need type annotation for '_repo'
montydb/montydb/storage/memory.py:15: error: Need type annotation for '_config'
montydb/montydb/storage/memory.py:94: error: Cannot assign to a method
montydb/montydb/storage/memory.py:94: error: Incompatible types in assignment (expression has type "Type[MemoryDatabase]", variable has type "Callable[[AbstractStorage], Any]")
montydb/montydb/storage/memory.py:146: error: Cannot assign to a method
montydb/montydb/storage/memory.py:146: error: Incompatible types in assignment (expression has type "Type[MemoryCollection]", variable has type "Callable[[AbstractDatabase], Any]")
montydb/montydb/storage/memory.py:167: error: Cannot assign to a method
montydb/montydb/storage/memory.py:167: error: Incompatible types in assignment (expression has type "Type[MemoryCursor]", variable has type "Callable[[AbstractCollection], Any]")
montydb/montydb/configure.py:24: error: Need type annotation for '_session' (hint: "_session: Dict[<type>, <type>] = ...")
montydb/montydb/storage/sqlite.py:313: error: Cannot assign to a method
montydb/montydb/storage/sqlite.py:313: error: Incompatible types in assignment (expression has type "Type[SQLiteDatabase]", variable has type "Callable[[AbstractStorage], Any]")
montydb/montydb/storage/sqlite.py:334: error: Argument 1 to "_ensure_table" has incompatible type "Callable[[SQLiteCollection, Any, Any], Any]"; expected "SQLiteCollection"
montydb/montydb/storage/sqlite.py:351: error: Argument 1 to "_ensure_table" has incompatible type "Callable[[SQLiteCollection, Any, Any, Any], Any]"; expected "SQLiteCollection"
montydb/montydb/storage/sqlite.py:414: error: Cannot assign to a method
montydb/montydb/storage/sqlite.py:414: error: Incompatible types in assignment (expression has type "Type[SQLiteCollection]", variable has type "Callable[[AbstractDatabase], Any]")
montydb/montydb/storage/sqlite.py:435: error: Cannot assign to a method
montydb/montydb/storage/sqlite.py:435: error: Incompatible types in assignment (expression has type "Type[SQLiteCursor]", variable has type "Callable[[AbstractCollection], Any]")
montydb/montydb/storage/lightning.py:171: error: Cannot assign to a method
montydb/montydb/storage/lightning.py:171: error: Incompatible types in assignment (expression has type "Type[LMDBDatabase]", variable has type "Callable[[AbstractStorage], Any]")
montydb/montydb/storage/lightning.py:191: error: Argument 1 to "_ensure_table" has incompatible type "Callable[[LMDBCollection, Any, Any, Any], Any]"; expected "LMDBCollection"
montydb/montydb/storage/lightning.py:201: error: Argument 1 to "_ensure_table" has incompatible type "Callable[[LMDBCollection, Any, Any, Any, Any], Any]"; expected "LMDBCollection"
montydb/montydb/storage/lightning.py:241: error: Cannot assign to a method
montydb/montydb/storage/lightning.py:241: error: Incompatible types in assignment (expression has type "Type[LMDBCollection]", variable has type "Callable[[AbstractDatabase], Any]")
montydb/montydb/storage/lightning.py:261: error: Cannot assign to a method
montydb/montydb/storage/lightning.py:261: error: Incompatible types in assignment (expression has type "Type[LMDBCursor]", variable has type "Callable[[AbstractCollection], Any]")
montydb/montydb/storage/flatfile.py:193: error: Cannot assign to a method
montydb/montydb/storage/flatfile.py:193: error: Incompatible types in assignment (expression has type "Type[FlatFileDatabase]", variable has type "Callable[[AbstractStorage], Any]")
montydb/montydb/storage/flatfile.py:220: error: Argument 1 to "_ensure_table" has incompatible type "Callable[[FlatFileCollection, Any, Any], Any]"; expected "FlatFileCollection"
montydb/montydb/storage/flatfile.py:233: error: Argument 1 to "_ensure_table" has incompatible type "Callable[[FlatFileCollection, Any, Any, Any], Any]"; expected "FlatFileCollection"
montydb/montydb/storage/flatfile.py:275: error: Cannot assign to a method
montydb/montydb/storage/flatfile.py:275: error: Incompatible types in assignment (expression has type "Type[FlatFileCollection]", variable has type "Callable[[AbstractDatabase], Any]")
montydb/montydb/storage/flatfile.py:298: error: Cannot assign to a method
montydb/montydb/storage/flatfile.py:298: error: Incompatible types in assignment (expression has type "Type[FlatFileCursor]", variable has type "Callable[[AbstractCollection], Any]")
Found 34 errors in 6 files (checked 100 source files)
davidlatwe commented 3 years ago

Link this to #27 . Drop Python 2 support in next minor release.

davidlatwe commented 3 years ago

Since #28 is on going, maybe we should start this after #28 is merged so to avoid merging conflicts. But I am not sure when I am able to back on working #28, hopefully in couple weeks.

sousajo-cc commented 3 years ago

Can I help on this One?

davidlatwe commented 3 years ago

Hi @sousajf1 , sure. :)

I am not sure when I can finish #28 so yeah, we should star this one.

Happy to see improvements!

madeinoz67 commented 3 years ago

seeing you are dropping python 2, I'll remove from dependencies in #57

so supported python versions are 3.7-3.9 ?

davidlatwe commented 3 years ago

so supported python versions 3.7-3.9 ?

Should be 3.6-3.9, which are versions being tested in Github Actions.

madeinoz67 commented 3 years ago

so supported python versions 3.7-3.9 ?

Should be 3.6-3.9, which are versions being tested in Github Actions.

hmm I could only see python-version: [ 2.7, 3.7, 3.8, 3.9 ] in your python-package workflow

davidlatwe commented 3 years ago

Ouch, I was referencing that info from README ! 🤕 Hmmm, Python 3.6 should be supported, it was in the Azure test before I change to use GitHub Action not long ago.

Could you add it back to the workflow ?

davidlatwe commented 3 years ago

Could you add it back to the workflow ?

Hey @madeinoz67 sorry, never mind, just created #59 to see if Python 3.6 still pass the tests.

bobuk commented 2 years ago

looks like since v2.4.0 is released there are no deal-breakers to add types hinting?

davidlatwe commented 2 years ago

Yeah, that's right! 😄

cclauss commented 2 years ago

It is easier to add type hints if Python 3.6 is dropped because then from __future__ import annotations can be used to substantially reduce the importing of standard data types (ex. Dict, List, Set, Tuple) from the typing module and to enable the more readable list[str | int | None] syntax. That import is only available on Python >= 3.7. Type hints can still be added on Py3.6 but they will be more verbose.

davidlatwe commented 3 months ago

Hi, sorry for bumping this old issue, just wanted to add this note for future self:

We might be able to gain some speed boost with mypyc once we added type hints.