Skip to content

Conversation

dnwpark
Copy link
Contributor

@dnwpark dnwpark commented Oct 2, 2025

Allows users to cast between types:

assert client.get(std.str.cast(std.int64(1))) == "1"
assert client.get(std.array[std.str].cast(
    std.array[std.int64](
        [std.int64(1), std.int64(2), std.int64(3)]
    )
) == ["1", "2", "3"]

Supports scalars, enums, array, tuple, range. Multiranges not supported, not sure we can even construct them in the qb right now.

@dnwpark dnwpark force-pushed the type-name branch 7 times, most recently from d509ac2 to 0327ede Compare October 3, 2025 01:53
@dnwpark dnwpark force-pushed the qb-cast branch 2 times, most recently from 4aff5a3 to 01836fa Compare October 3, 2025 16:13
@dnwpark dnwpark changed the base branch from type-name to master October 3, 2025 16:16
@dnwpark dnwpark force-pushed the qb-cast branch 3 times, most recently from 2b68a56 to e383581 Compare October 3, 2025 20:00
@dnwpark dnwpark marked this pull request as ready for review October 3, 2025 20:17
@msullivan
Copy link
Member

Is the idea here that we support generating casts between any two types, and then we let the server deal with figuring out which are real?

That's probably a reasonable first take.
I think eventually we might want to generate overloads based on reflecting the casts in the schema, but this is an expedient first step.

@elprans @1st1 thoughts?

@dnwpark dnwpark force-pushed the qb-cast branch 2 times, most recently from f473ef1 to 67d8d2f Compare October 11, 2025 02:33
Comment on lines +2529 to +2537
if gt in COLLECTION_TYPES:
# Import collection classes directly so mypy sees them as
# generic classes, not type aliases
self.write(
f"from {rel_import.module} "
f"import {type_ident} as {type_ident}"
)
else:
self.write(f"{type_ident} = {imported_name}")
Copy link
Member

Choose a reason for hiding this comment

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

What happens if you don't do this?
I think it shouldn't matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mypy complains about applying the type arguments to std.tuple without this:

tests/test_qb_cast_tuple_01.py:20: error: Bad number of arguments for type alias, expected 0, given 4  [type-arg]

It's ok with std.array though, for some reason...

self.write()
self.write_type_reflection(stype)

# cast method
Copy link
Member

Choose a reason for hiding this comment

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

Maybe generate these in a separate mtehod?

tests/test_qb.py Outdated
# scalar to enum
from models.orm import default, std

result = self.client.get(default.Color.cast(std.str("Red")))
Copy link
Member

Choose a reason for hiding this comment

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

This is an example of where I feel the explicit cast is inconsistent. We are casting a Python literal string "Red" to std.str via a (Pythonic) call to a constructor, whereas the second cast is via a classmethod.

Copy link
Member

@elprans elprans left a comment

Choose a reason for hiding this comment

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

I feel like this need a bit more work. Ideally we should expose all explicit casts with support for base Python types, e.g the Enum casts should accept Python strings directly without an extra std.str cast.

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.

3 participants