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

py.typed #257

Open
fried opened this issue Sep 29, 2020 · 4 comments
Open

py.typed #257

fried opened this issue Sep 29, 2020 · 4 comments

Comments

@fried
Copy link

fried commented Sep 29, 2020

Have you considered providing a init.pyi file for the lmdb package?

https://www.python.org/dev/peps/pep-0561/#packaging-type-information

@jnwatson
Copy link
Owner

I have. It is on the list to do. PRs are always welcome. :)

@jnwatson
Copy link
Owner

(Leaving this open as a reminder)

@fried
Copy link
Author

fried commented Oct 28, 2020

I have a local lmdb.pyi to make pylance (pyright) happy, but it only has the stuff I use. The hardest part was Cursor.iternext because it changes the return type based on arguments (the other iter methods will need similar treatment).

If I have time i'll see if I can fill out the pyi fully and submit a pull. But If I don't make it and somebody takes up the mantle here is the hard parts. Also we would have to probably use lmdb.__init__.pyi

class Cusor:
    @overload
    def iternext(self, keys: Literal[False]) -> Iterator[bytes]:
        ...

    @overload
    def iternext(self, keys: Literal[False], values: Literal[True]) -> Iterator[bytes]:
        ...

    @overload
    def iternext(self, values: Literal[False]) -> Iterator[bytes]:
        ...

    @overload
    def iternext(self, keys: Literal[True], values: Literal[False]) -> Iterator[bytes]:
        ...

    @overload
    def iternext(
        self, keys: Literal[True], values: Literal[True]
    ) -> Iterator[tuple[bytes, bytes]]:
        ...

    @overload
    def iternext(self) -> Iterator[tuple[bytes, bytes]]:
        ...

Oh and txn.get is a little complex, also included put so you can see how I indicated keyword only arguments to keep it straight in my head

T = TypeVar("T")

class DatabaseHandle: ...  #  _Database so people can reference the type without pylint complaining about protected. 

class Transaction:
    def get(
        self,
        key: bytes,
        default: Optional[T] = ...,
        *,  
        db: Optional[DatabaseHandle] = ...,
    ) -> Union[Optional[T], bytes]:
        ...

    def put(
        self,
        key: bytes,
        value: bytes,
        *,
        db: Optional[DatabaseHandle] = ...,
        dupdata: bool = ...,
        overwrite: bool = ...,
    ) -> bool:
        ...

@jnwatson
Copy link
Owner

Thanks! I'll have to check out pylance/pyright.

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