Skip to content

Added SQLAlchemy annotations - #1029

Merged
JelleZijlstra merged 3 commits into
python:masterfrom
Stiivi:sqlalchemy-annotations
Mar 22, 2017
Merged

JelleZijlstra merged 3 commits into
python:masterfrom
Stiivi:sqlalchemy-annotations

Conversation

@Stiivi

@Stiivi Stiivi commented Mar 19, 2017

Copy link
Copy Markdown
Contributor
  • Made Connection and Engine sublcasses of Connectable (sqlalchemy Engine and Connection should be subclasses of Connectable #1018)
  • Moved execute() from Connection to Connectable
  • Made RowProxy a Mapping and removed Mapping inherited methods
  • Made ResultProxy an Iterator of RowProxy
  • Added most relevant methods for fetching of ResultProxy
  • Added where(), group_by(), order_by() and limit() to Select

* Made Connection and Engine sublcasses of Connectable (#1018)
* Moved execute() from Connection to Connectable
* Made RowProxy a Mapping and removed Mapping inherited methods
* Made ResultProxy an Iterator of RowProxy
* Added most relevant methods for fetching of ResultProxy
* Added where(), group_by(), order_by() and limit() to Select
def close(self) -> None: ...
def __iter__(self) -> Iterator[RowProxy]: ...
def fetchall(self) -> Iterator[RowProxy]: ...
def fetchmany(self, size: int=None) -> Iterator[RowProxy]: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Better to write Optional[int] = ...

def fetchmany(self, size: int=None) -> Iterator[RowProxy]: ...
def fetchone(self) -> Optional[RowProxy]: ...
def first(self) -> Optional[RowProxy]: ...
def scalar(self): ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does this return?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Very likely an union of DB API Type Objects. By brief search of this repo they don't seem to be defined anywhere to be shared except as internal part of sqlite and mysql.

Would it be ok to use Any with a comment reference to future union type?

This is also related to the RowProxy value type which should be of this union.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think returning Any explicitly with a comment is better because it shows that we've thought about it. (I'm hoping we can disallow missing types in typeshed at some point.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added an issue, so we won't forget: #1037.

class Select(HasPrefixes, HasSuffixes, GenerativeSelect): ...

class Select(HasPrefixes, HasSuffixes, GenerativeSelect):
def where(self, whereclause) -> Select: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any way these can have more specific types?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, I plan to add them later once I get to the code that is using them.

from typing import Any, Iterable, Iterator, List, Mapping, Optional, Tuple

# Dummy until I figure out something better.
class Connectable:

@JelleZijlstra JelleZijlstra Mar 19, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This class actually has numerous additional methods and it lives in interface.py, not base.py. I'm not sure how valuable it is to add those additional methods, but in general it might be worse to have incomplete stubs (missing important functions) than no stubs, because with incomplete stubs people will get false positive errors about missing functions. It sounds like that's currently happening with SQLAlchemy (#974).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So what is your suggestion here? Add all the Connectable methods and move it into interface.pyi?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That would be better yes. Not sure how many additional missing methods would have to be added.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll prepare update to this PR.

* Created sqlalchemy.engine.result and moved ResultProxy and RowProxy
  there
* Created sqlalchemy.engine.interfaces and moved Connectable there
* Added non-deprecated methods to Connectable: connect,
  contextual_connect and scalar
* Fixed return type of scalar() to Any

# Note: The return type `Any` should be a DB API 2 value type once defined
# TODO: See #1037
def scalar(self): ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you put -> Any explicitly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Had it in Connectable, thought the other was fixed already.

... had it in Connectable only.
@JelleZijlstra

Copy link
Copy Markdown
Member

Thanks! I'm going to merge this since it makes things better, but depending on what happens in #974 we may end up making bigger changes.

@JelleZijlstra
JelleZijlstra merged commit 5b47bc9 into python:master Mar 22, 2017
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.

2 participants