Skip to content

Pymysql cursor types and fetching return types #5652

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

Merged
merged 13 commits into from
Jun 17, 2021
Merged

Conversation

Akuli
Copy link
Collaborator

@Akuli Akuli commented Jun 16, 2021

Fixes #5567 so that Cursor and DictCursor no longer return annoying unions. I also got rid of several Optionals. In those methods, I looked at the source code and I tried select * from foo on a local test database, and couldn't get it to return None.

I also made Connection generic, so that when you create a connection, it remembers which cursor class you want to use. You can still customize this by passing a custom class to .cursor().

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't compared with the implementation, but the changes look reasonable. Especially getting rid of the response unions. Two remarks below.

Also, could you also update the version in METADATA.toml?

@Akuli Akuli requested a review from srittau June 17, 2021 18:46
@srittau srittau merged commit c5b59b9 into python:master Jun 17, 2021
@Akuli Akuli deleted the pymysql branch June 17, 2021 18:54
@Alch-Emi
Copy link

Alch-Emi commented Jul 8, 2021

I'm having some problems with this, but I'm posting here instead of creating a new issue in case I'm doing something dumb. I'm currently writing some typed code that uses a Connection[Cursor], but when I include this in my function signature, I get the error TypeError: 'type' object is not subscriptable, presumably since the underlying library doesn't expect Connection to be a generic.

I was able to fix this either by patching pymysql to use a generic Connection, or by removing the generic from my type signature. The former solution of course makes the code much harder to maintain, and would require a fork of pymysql specifically for this project, at least until pymysql adopts python 3.10 or 3.7 (See PyMySQL/PyMySQL#953). The later causes my typechecker to complain very loudly about how Connection is supposed to be a generic, and also removes the benefit of having the generic in the first place.

Is there a solution to this that doesn't require either leaving a type error in the code or forking pymysql?

@Akuli
Copy link
Collaborator Author

Akuli commented Jul 8, 2021

On 3.7+, add to beginning of file:

from __future__ import annotations

This tells Python to not evaluate the type annotations, so that they are just for the type checker. It also means that you can also use Foo | None instead of Optional[Foo], for example.

@Alch-Emi
Copy link

Alch-Emi commented Jul 8, 2021

Oh thank you so much! That's perfect!

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

Successfully merging this pull request may close these issues.

pymysql: fetch result type
3 participants