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

implement pep 646 for select() / query() / Row / Result / execute() / scalars() #7516

Closed
zzzeek opened this issue Dec 29, 2021 · 11 comments
Closed
Labels
big job a major change that requires a lot of knowledge and work duplicate This issue or pull request already exists typing pep -484 typing issues. independent of "mypy"
Milestone

Comments

@zzzeek
Copy link
Member

zzzeek commented Dec 29, 2021

pep 646 has been out for over a year and is in python 3.10

https://www.python.org/dev/peps/pep-0646/#summary-examples

i dont think mypy has support . however, maybe pylance / pyright do. if they do, this would be great, we can just go for it. this issue is moved from sqlalchemy/sqlalchemy2-stubs#1 .

@zzzeek zzzeek added the typing pep -484 typing issues. independent of "mypy" label Dec 29, 2021
@zzzeek zzzeek added this to the 2.0 milestone Dec 29, 2021
@zzzeek zzzeek added this to 2.0 deliverables backlog in sqlalchemy 2 via automation Dec 29, 2021
@CaselIT
Copy link
Member

CaselIT commented Dec 29, 2021

however, maybe pylance / pyright do. if they do, this would be great, we can just go for it.

From the pyright readme it seems they indeed do support it: https://github.com/Microsoft/pyright#type-checking-features

@CaselIT CaselIT added the big job a major change that requires a lot of knowledge and work label Dec 29, 2021
@zzzeek
Copy link
Member Author

zzzeek commented Dec 31, 2021

OK, change of plans, from what I am seeing.

There does not seem to be pep-646 implemented anywhere.

Even though the pep says it's in Python 3.10, nope:

$ python
Python 3.10.0 (default, Nov  5 2021, 17:23:47) [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> typing.TypeVarTuple
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'typing' has no attribute 'TypeVarTuple'
>>> import typing_extensions
>>> typing_extensions.TypeVarTuple
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'typing_extensions' has no attribute 'TypeVarTuple'

additionally the examples given don't even compile in Python:

$ python test3.py 
  File "/home/classic/dev/sqlalchemy/test3.py", line 9
    class Array(Generic[*Shape]):
               ^
SyntaxError: expected ':'

taht's using this example:

from typing import TypeVar, Generic, Tuple, NewType
from typing_extensions import TypeVarTuple

Shape = TypeVarTuple("Shape")

Height = NewType('Height', int)
Width = NewType('Width', int)

class Array(Generic[*Shape]):

    def __init__(self, shape: Tuple[*Shape]):
        self._shape: Tuple[*Shape] = shape

    def get_shape(self) -> Tuple[*Shape]:
        return self._shape

shape = (Height(480), Width(640))
x: Array[Height, Width] = Array(shape)

y = abs(x)  # Inferred type is Array[Height, Width]
z = x + x   #        ...    is Array[Height, Width]

so....even if they did implement this in 3.11, i dont know what we're going to do because the syntax isn't even supported?!

@zzzeek
Copy link
Member Author

zzzeek commented Dec 31, 2021

i've asked microsoft/pyright#2780

@CaselIT
Copy link
Member

CaselIT commented Dec 31, 2021

Co basically it's supported but you cannot actually use it anywhere because python doesn't support it?

@MaicoTimmerman MaicoTimmerman moved this from 2.0 deliverables backlog to 2.0 deliverables done in sqlalchemy 2 Dec 31, 2021
@MaicoTimmerman MaicoTimmerman moved this from 2.0 deliverables done to 2.0 deliverables backlog in sqlalchemy 2 Dec 31, 2021
@zzzeek
Copy link
Member Author

zzzeek commented Dec 31, 2021

i got a POC to work on the above pyright issue. blows up mypy obviously

@CaselIT
Copy link
Member

CaselIT commented Dec 31, 2021

Oh nice, is it on gerrit already?

@zzzeek
Copy link
Member Author

zzzeek commented Dec 31, 2021

not yet. it "works" but doesnt really do what we need it to, and i dont know if any of these PEPs have plans in mind that would allow it. ill put it up in a moment

@sqla-tester
Copy link
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

attempt pep 646 https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3446

@zzzeek zzzeek added awaiting pep there's no PEP to do what we want to do wontfix / out of scope something we decided we aren't doing, for whatever reason labels Jan 3, 2022
@zzzeek
Copy link
Member Author

zzzeek commented Jan 3, 2022

OK based on what we see at python/typing#1001, the answer is, "can't be done". I'm not really sure what the usefulness of pep646 is at the moment if the types within the tuples are inaccessible.

For the moment I dont see any way of SQLAlchemy statements being typed in a way that is meaningful in terms of the kind of result/ row they produce.

I know SQLModel does this really hacky trick to try to give users the illusion that select statements are typed to their contents, though I'm not sure that would even be workable unless you also have a more automatic version of session.execute(), then sqlalchemy-stubs is also doing a version of it. For those particular hacks, maybe we can get typing for a Query object and a select() object against a single ORM entity, then have it translate out to session.scalars() / query.all(). That is we support single-element ScalarResult etc. would be better than nothing.

@zzzeek zzzeek closed this as completed Jan 3, 2022
sqlalchemy 2 automation moved this from 2.0 deliverables backlog to 2.0 deliverables done Jan 3, 2022
@CaselIT
Copy link
Member

CaselIT commented Jan 3, 2022

shame :(

@zzzeek zzzeek added duplicate This issue or pull request already exists and removed wontfix / out of scope something we decided we aren't doing, for whatever reason awaiting pep there's no PEP to do what we want to do labels Nov 15, 2023
@zzzeek
Copy link
Member Author

zzzeek commented Nov 15, 2023

this will be done and tracked in #10635

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
big job a major change that requires a lot of knowledge and work duplicate This issue or pull request already exists typing pep -484 typing issues. independent of "mypy"
Projects
No open projects
sqlalchemy 2
  
2.0 deliverables done
Development

No branches or pull requests

3 participants