Skip to content

Type hints and mypy support? #977

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

Closed
fgimian opened this issue Sep 25, 2019 · 9 comments
Closed

Type hints and mypy support? #977

fgimian opened this issue Sep 25, 2019 · 9 comments

Comments

@fgimian
Copy link

fgimian commented Sep 25, 2019

Hey there guys, hope you're doing well! 😄

Are there any plans to add typing information and support mypy in the future?

Kindest Regards
Fotis

@dvarrazzo
Copy link
Member

It would be lovely! Would you like to work on it?

@fgimian
Copy link
Author

fgimian commented Sep 25, 2019

It would be lovely! Would you like to work on it?

I may give it a try soon, we're currently integrating typing into our projects so I'm still getting my head around various aspects.

The main concern would probably be Python 2.7 compatibility as it would be preferred to have the types inline with the code but I'm pretty sure Python 2.7 won't like that.

Peraps if the next major version is Python 3.x compatible only, that will make it a lot easier 😄

Thanks a lot for the reply 😄
Fotis

@ltworf
Copy link

ltworf commented Sep 25, 2019

You can create separate pyi files to give to mypy if you don't want to inline in the code.

@septatrix
Copy link

Are there any plans yet to drop support for Python 2 in e.g. psycopg3 as I would welcome that change

@dvarrazzo
Copy link
Member

Psycopg3 is Python >= 3.6 only, and it's being developed at mypy --strict already. There is a writeup here.

@bersace
Copy link

bersace commented Apr 13, 2021

Does anyone have some type stubs for psycopg2 ? I'm new to pyright and I have error: "DatabaseError" is unknown import symbol all the way around. It looks like pyright doesn't find some types hints.

@doublethink13
Copy link

Maybe this types package is relevant to someone

@Avasam
Copy link

Avasam commented Apr 10, 2025

With support for Python 3.7 dropped in #1729, it should be easy enough to merge typeshed's stub's annotations into psycopg2's source code.
Here's a few tools that may help with that: https://typing.python.org/en/latest/#type-hint-and-stub-integration
Slap a few from __future__ import annotations at the top of modules, wrap imports of typing_extensions and _typeshed in if TYPE_CHECKING blocks and you're mostly good to go.

Then at least run https://github.com/python/mypy for sanity checking (you can disable rules that fail related to checking psycopg's own code rather than just the annotations' validity)

And maybe Ruff's flake8-future-annotations (FA), late-future-import (F404), flake8-pyi (PYI) and some pyupgrade (UP) which can help staying up to date with annotation best practices.

@dvarrazzo
Copy link
Member

@Avasam, I have experience of porting large codebases to type checking and I can confidently say that you don't know what you are talking about. Psycopg 3 had to be designed ground-up to support type checking (introducing it when the codebase was 3 weeks old was already a massive work), it's not a thing you can slap on a project not designed for it and especially not running a bunch of code linters.

If you want type checking you either use psycopg 3 or you pay me by the hour so that I can provide you some sub-optimal type annotation for a C library that was designed 15 years before type hinting was a thing in Python.

This topic doesn't require further discussion. If you want badly code annotation, my email is public and I will be happy to quote you a few days of work.

@psycopg psycopg locked as resolved and limited conversation to collaborators Apr 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants