Skip to content

Problems with using Union types #766

Open

Description

@variux

Hi

To give a general idea I'm working on a wrapper for sqlite3 in codon, a function takes a parameter that can takes multiple types

Ex: str, int, bool

But this little code breaks

`Param = Union[str, int, bool]
value: Param = "test"

print(type(value))`

$ codon run test.py union.codon:27 (44-45): error: __ptr__() only takes identifiers or tuple fields as arguments ├─ union.codon:92 (28-64): error: during the realization of _get_data(u: TypeWrap[Union[bool | int | str]], T: bool) ├─ str.codon:27 (20-36): error: during the realization of _str(union: TypeWrap[Union[bool | int | str]]) ├─ builtin.codon:28 (27-33): error: during the realization of __new__(what: TypeWrap[Union[bool | int | str]]) ╰─ test.py:4 (1-6): error: during the realization of print(*args: Tuple[TypeWrap[Union[bool | int | str]]], sep: str, end: str, file: Ptr[byte], flush: bool)

Activity

  1. arshajii commented on Mar 4, 2026

    @arshajii
    Contributor

    This seems to be an issue with printing the union type itself, since the first two lines do work. You can also use e.g. isinstance(param, str) to check for a particular type.

    By the way, you can do this fully at compile-time via a pattern like:

    def foo(param):
        if isinstance(param, str):
            ...
        elif isinstance(param, int):
            ...
        elif isinstance(param, bool):
            ...
        else:
            # error

    If you don't give param an explicit union type, then Codon can deduce the type at compile-time and only compile the appropriate if/elif branch.

  2. variux commented on Mar 4, 2026

    @variux
    Author

    I wanted to print the type to say Type {type} is not supported but even being able to print it, it doesn't print the actual type

  3. yfinkelstein commented on Mar 7, 2026

    @yfinkelstein

    Related: #738

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions