Skip to content

Pymysql cursor types and fetching return types - #5652

Merged
srittau merged 13 commits into
python:masterfrom
Akuli:pymysql
Jun 17, 2021
Merged

srittau merged 13 commits into
python:masterfrom
Akuli:pymysql

Conversation

@Akuli

@Akuli Akuli commented Jun 16, 2021

Copy link
Copy Markdown
Collaborator

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().

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

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?

Comment thread stubs/PyMySQL/pymysql/connections.pyi Outdated
Comment thread stubs/PyMySQL/pymysql/connections.pyi Outdated
@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

alch-emi commented Jul 8, 2021

Copy link
Copy Markdown

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

Akuli commented Jul 8, 2021

Copy link
Copy Markdown
Collaborator Author

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

alch-emi commented Jul 8, 2021

Copy link
Copy Markdown

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