-
Notifications
You must be signed in to change notification settings - Fork 0
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
Issue 6810 type annotations for sqlalchemy.orm.mapped_collection #6
Issue 6810 type annotations for sqlalchemy.orm.mapped_collection #6
Conversation
m._get_state_attr_by_column(state, state.dict, col) | ||
for col in self._cols(m) | ||
] | ||
if self.composite: | ||
return tuple(key) | ||
else: | ||
return key[0] | ||
return key[0] # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KeyedColumnElement
is actually from sql.elements
and its key
attribute is of str
type - so it might be that the elements in key
list here are strings. We probably can't do much more here, since _get_state_attr_by_column()
has Mike's annotations - just wanted to add a bit of context from sql.elements
.
9dc33c3
to
d580a92
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing more from my end, good job!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I think Mike will offer to remove # type: ignore
s.
self, | ||
) -> tuple[ | ||
Callable[[_KT, _KT], KeyFuncDict[_KT, _KT]], | ||
tuple[Any, dict[_KT, _KT] | dict[_KT, _KT]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| dict[_KT, _KT]
looks redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also |
is not supported by Python 3.7 which SQLA must support. So only Union[]
can be used.
@@ -223,7 +256,7 @@ def attribute_keyed_dict( | |||
|
|||
|
|||
def keyfunc_mapping( | |||
keyfunc: Callable[[Any], _KT], | |||
keyfunc: Callable[[_KT], _KT], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest creating aliases for Callable[[_KT], _KT]
and KeyFuncDict[_KT, _KT]
and reuse them.
For |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sqlalchemy/lib/sqlalchemy/orm/mapped_collection.py
Lines 117 to 121 in e0a6cac
def _table_key(c: KeyedColumnElement[_KT]) -> Optional[_KT]: | |
if not isinstance(c.table, expression.TableClause): | |
return None | |
else: | |
return c.table.key # type: ignore |
sqlalchemy/lib/sqlalchemy/orm/mapped_collection.py
Lines 185 to 189 in e0a6cac
cols = [ | |
coercions.expect(roles.ColumnArgumentRole, q, argname="mapping_spec") | |
for q in util.to_list(mapping_spec) | |
] | |
keyfunc = _PlainColumnGetter(cols) # type: ignore |
These 2 places make me really scratch my head. Since in the 2nd snippet we have cols: list[ColumnElement]
but in the 1st one we eventually expect KeyedColumnElement
.
Maybe there's a way to loosen all of the instances of KeyedColumnElement
throughout the module up to plain ColumnElement
and then create a Union[ColumnElement, KeyedColumnElement]
with an assert
narrowing the type down in the final branch of the 1st snippet, since this is the only place that actually expects a KeyedColumnElement
.
Are these KeyedColumnElement
types just run-time collected or there's a reason to have them that strict in the whole module?
Maybe it's also possible to add another @overload
for coercions.expect
to narrow cols
down to KeyedColumnElement
in the 2nd snippet and leave all of the KeyedColumnElement
intact.
Not sure if I'm just overthinking it and it's OK to just leave it type: ignore
'ed or it's a legitimate way of thinking.
Overall I'm pretty much done with reviewing the module and I think that all of the rest instances of type: ignore
can be left in place. Just try to consider this and my previous comment if it doesn't take too much time and ship it off to the upstream repo.
e0a6cac
to
fcd123b
Compare
fc109bc
to
a46a5db
Compare
PR was merged to original repo, sqlalchemy#9140, closing this one |
Description
An attempt to annotate lib/sqlalchemy/orm/mapped_collection.py with type hints (issue sqlalchemy#6810)
Checklist
This pull request is:
must include a complete example of the issue. one line code fixes without an
issue and demonstration will not be accepted.
Fixes: #<issue number>
in the commit messageinclude a complete example of how the feature would look.
Fixes: #<issue number>
in the commit messageHave a nice day!