Skip to content

Range type does not implement __contains__ #12093

Closed
@RazerM

Description

@RazerM
Contributor

Describe the use case

I'm upgrading from SQLAlchemy 1.4 where the psycopg2 range types are used, and Range.__contains__ is implemented for those.

Databases / Backends / Drivers targeted

PostgreSQL dialect

Example Use

the psycopg2 types implement it, but if selecting a column that is e.g. INT4RANGE then the sqlalchemy Range type is returned:

>>> range1 = conn.scalar(select(func.int4range(1, 10)))
>>> range1
NumericRange(1, 10, '[)')
>>> 3 in range1
True
>>> 12 in range1
False

>>> range2 = conn.scalar(select(type_coerce(func.int4range(1, 10), INT4RANGE)))
>>> range2
Range(lower=1, upper=10, bounds='[)', empty=False)
>>> 3 in range2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'Range' is not iterable

Additional context

I'll open a PR

Activity

added
requires triageNew issue that requires categorization
use casenot really a feature or a bug; can be support for new DB features or user use cases not anticipated
on Nov 13, 2024
changed the title [-]Implement Range.__contains__[/-] [+]Range type does not implement __contains__[/+] on Nov 13, 2024
added and removed
requires triageNew issue that requires categorization
on Nov 14, 2024
added this to the 2.0.x milestone on Nov 14, 2024
sqla-tester

sqla-tester commented on Nov 15, 2024

@sqla-tester
Collaborator

Frazer McLean has proposed a fix for this issue in the main branch:

Add Range.contains https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5596

sqla-tester

sqla-tester commented on Nov 15, 2024

@sqla-tester
Collaborator

Frazer McLean has proposed a fix for this issue in the rel_2_0 branch:

Add Range.contains https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5597

added a commit that references this issue on Nov 18, 2024
9732f15
added a commit that references this issue on Nov 18, 2024
31975cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    postgresqluse casenot really a feature or a bug; can be support for new DB features or user use cases not anticipated

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @RazerM@sqla-tester@CaselIT

      Issue actions

        Range type does not implement __contains__ · Issue #12093 · sqlalchemy/sqlalchemy