Skip to content

Connor/eng 1914 ensure select works with all ways of querying sqlalchemy#15

Closed
conniedonahue wants to merge 7 commits intomainfrom
connor/eng-1914-ensure-select-works-with-all-ways-of-querying-sqlalchemy
Closed

Connor/eng 1914 ensure select works with all ways of querying sqlalchemy#15
conniedonahue wants to merge 7 commits intomainfrom
connor/eng-1914-ensure-select-works-with-all-ways-of-querying-sqlalchemy

Conversation

@conniedonahue
Copy link
Copy Markdown
Contributor

@conniedonahue conniedonahue commented Jul 2, 2025

Essentially this adds the same sort of overrides to select() and execute() as those we have in Query

@linear
Copy link
Copy Markdown

linear Bot commented Jul 2, 2025

ENG-1914 Ensure `Select` works with all ways of querying `SQLAlchemy`, even ones we don't support

i.e. they can't use our authorized method, but they can use other methods with weird queries that authorized wouldn't support

@conniedonahue conniedonahue requested a review from vrama628 July 2, 2025 02:14
@conniedonahue conniedonahue marked this pull request as draft July 2, 2025 02:15
@conniedonahue conniedonahue removed the request for review from vrama628 July 2, 2025 02:15
@conniedonahue
Copy link
Copy Markdown
Contributor Author

Since this is basically just an extension of the above quoted PR, I'm going to hold this off in draft state until I see notes on that one!

@conniedonahue conniedonahue force-pushed the connor/eng-1914-ensure-select-works-with-all-ways-of-querying-sqlalchemy branch from 68a4c01 to d1fa03a Compare July 10, 2025 00:16
@conniedonahue conniedonahue marked this pull request as ready for review July 10, 2025 00:33
@conniedonahue conniedonahue requested a review from Copilot July 10, 2025 00:33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds type-safe overloads for SQL execution and selection to support various query forms with the custom Select and Session.execute.

  • Extends Session.execute with overloads for 1–4 tuple returns and a fallback for other cases.
  • Makes Select generic and provides overloads on the select() factory for 1–4 entities plus a varargs fallback.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/sqlalchemy_oso_cloud/session.py Imported Result and TypedReturnsRows, added multiple execute overloads; overridden execute calls super with filtering support.
src/sqlalchemy_oso_cloud/select_impl.py Made Select generic, added overloads for the select() helper and updated its varargs implementation.
Comments suppressed due to low confidence (2)

src/sqlalchemy_oso_cloud/session.py:84

  • Consider adding unit tests for each execute overload (1–4 columns and the fallback) to verify that authorization filtering is applied correctly across all cases.
  # Execute overloads for our custom Select class

src/sqlalchemy_oso_cloud/session.py:9

  • TypeVars T2, T3, and T4 are used in the execute overloads but not declared. Add T2 = TypeVar("T2"), T3 = TypeVar("T3"), and T4 = TypeVar("T4") at the top of the file.
T1 = TypeVar("T1")

Comment thread src/sqlalchemy_oso_cloud/session.py Outdated

# Fallback for 5+ entities or any other Select
@overload
def execute(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there a reason this is a duplicate header of the below function?

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.

Good catch! This is a remnant of some slimming down I did of this section. I cut out this repeat!

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.

Ah! I misspoke! Instead of deleting this overload, I need to adjust the statement to Any to work as a true fallback.

Comment thread src/sqlalchemy_oso_cloud/session.py Outdated
When used with our custom Select class or Query class that have been filtered
with .authorized(), the authorization filtering will be applied.
"""
return super().execute( # type: ignore[misc]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there a reason we can't set the execution_options type to OrmExecuteOptionsParameter = EMPTY_DICT like how it is in the super class? then we don't need the type ignores

@irisjhu
Copy link
Copy Markdown
Contributor

irisjhu commented Jul 18, 2025

i believe this pr also wraps up this ticket, but cmiiw

Comment thread src/sqlalchemy_oso_cloud/session.py Outdated
Comment on lines +9 to +19
if TYPE_CHECKING:
from sqlalchemy.orm._typing import OrmExecuteOptionsParameter
from sqlalchemy.util._collections import EMPTY_DICT
else:
try:
from sqlalchemy.orm._typing import OrmExecuteOptionsParameter
from sqlalchemy.util._collections import EMPTY_DICT
except ImportError:
OrmExecuteOptionsParameter = Mapping[str, Any] # type: ignore[misc]
EMPTY_DICT = {} # type: ignore[assignment]

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.

I'm not sure if this is the right move, but I wanted to write a little defensively around OrmExecuteOptionsParameter and EMPTY_DICT since they are both pulled from SQLAlchemy's internal collections. With this logic, if they make some sort of breaking change that causes an ImportError, we have fallbacks.

@conniedonahue conniedonahue requested a review from irisjhu July 25, 2025 16:48
@conniedonahue
Copy link
Copy Markdown
Contributor Author

I'm just going to close this, not worth expending the time on pushing through a review.

@github-actions github-actions Bot locked and limited conversation to collaborators Dec 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants