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

Update orderinglist annotations, list compatibity #10889

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mjpieters
Copy link
Contributor

@mjpieters mjpieters commented Jan 15, 2024

Description

  • Don't omit the _T typevar in ordering_list and OrderingList.ordering_func; type checkers need to understand the relationship between the OrderingList instance and the ordering function connected to it.
  • The ordering function can return any value, not just integers
  • The ordering_attr argument to OrderingList is not optional
  • Update list methods to accept the same signature as the overridden methods, including SupportsIndex instead of int, an iterable of _T when using __setitem__ with a slice (and not just sequences) and converting the index value to an integer before passing it to the ordering_func callable.
  • Update __setitem__ to not attempt to handle slice objects as handling all edge cases of slice length and iterable length is very tricky and most use of OrderingList and slices is handled by the SQLAlchemy collections instrumentation anyway.
  • Remove the __setslice__ and __delslice__ methods, which were deprecated in Python 2.6 and removed in Python 3.0.

Fixes #10888

Checklist

This pull request is:

  • A documentation / typographical / small typing error fix
    • Good to go, no issue or tests are needed
  • A short code fix
    • please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue. one line code fixes without an
      issue and demonstration will not be accepted.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests. one line code fixes without tests will not be accepted.
  • A new feature implementation
    • please include the issue number, and create an issue if none exists, which must
      include a complete example of how the feature would look.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests.

- Don't omit the `_T` typevar in `ordering_list` and
  `OrderingList.ordering_func``; type checkers need to understand the
  relationship between the `OrderingList` instance and the ordering
  function connected to it.
- The ordering function can return _any_ value, not just integers
- The `ordering_attr` argument to `OrderingList` is not optional
- Update list methods to accept the same signature as the overridden
  methods, including `SupportsIndex` instead of `int`, an iterable of
  `_T` when using `__setitem__` with a slice (and not just sequences)
  and converting the index value to an integer before passing it to the
  `ordering_func` callable.
- Update `__setitem__` to _not_ attempt to handle slice objects as
  handling all edge cases of slice length and iterable length is very
  tricky and most use of `OrderingList` and slices is handled by
  the SQLAlchemy collections instrumentation anyway.
- Remove the `__setslice__` and `__delslice__` methods, which were
  deprecated in Python 2.6 and removed in Python 3.0.

Fixes sqlalchemy#10888
@mjpieters
Copy link
Contributor Author

For context: this started as a quick fix for the ordering_list() function annotations but snowballed from there.

@zzzeek
Copy link
Member

zzzeek commented Jan 15, 2024

are we going to add some typing tests ? these can go into https://github.com/sqlalchemy/sqlalchemy/tree/main/test/typing/plain_files/ext where we probably could have a new orderinglist.py module and/or folder.

Copy link
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

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

OK, this is sqla-tester setting up my work on behalf of CaselIT to try to get revision 314dbc6 of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Collaborator

New Gerrit review created for change 314dbc6: https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5118

@sqla-tester
Copy link
Collaborator

Michael Bayer (zzzeek) wrote:

good stuff, dont worry about that oracle failure. can we add a changelog file here that we applied full typing to orderinglist?

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5118

Copy link
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

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

Michael Bayer (zzzeek) wrote:

code review left on gerrit

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5118

# is far simpler.
# SQLAlchemy collection instrumentation otherwise takes care of
# slices in all normal use of OrderingList in ORM classes.
super().__setitem__(__index, __entity)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Michael Bayer (zzzeek) wrote:

is there a test that would fail with the old implementation? I missed that the original issue refers to multiple problems, some that are not typing. the changelog note should include this as well, likely as a separate changelog item completely (can still be in one file, multiple `.. change::`` directives can be in one file

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5118

Copy link

@Imran-imtiaz48 Imran-imtiaz48 left a comment

Choose a reason for hiding this comment

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

Your implementation demonstrates a clear understanding of advanced SQLAlchemy features, such as the ordering_list extension and custom ordering functions. The use of text_to_pos as an ordering function within pos_from_text effectively extracts numerical positions from Bullet text, showcasing your ability to handle complex ordering logic. The Slide and Bullet classes are well-defined, with proper relationships and use of mapped columns. Including the type-checking section with reveal_type ensures that the types of pos_from_text and slide.bullets are correctly inferred, which is crucial for maintaining type safety in your code. Overall, this code is a sophisticated and well-implemented example of leveraging SQLAlchemy's capabilities for ordered collections.

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.

OrderingList typing incorrect / incomplete and implementation doesn't accept non-int indices
5 participants