Skip to content

Conversation

arielf212
Copy link

@arielf212 arielf212 commented Feb 7, 2025

This PR adds the ability to use real unions (a la Union[int, str]), as requested in #461.

I am new to contributing to this Repository, and I'll happily add anything I might have missed (docs/tests/incorrect code location/etc).

@arielf212 arielf212 marked this pull request as draft February 7, 2025 13:44
@arielf212
Copy link
Author

I am setting this to draft for now since I have no idea whether what I did is enough, I'll happily convert this to a merge request once someone will give me the OK.

@svlandeg svlandeg added the feature New feature, enhancement or request label Feb 7, 2025
@arielf212 arielf212 marked this pull request as ready for review February 7, 2025 17:40
@arielf212
Copy link
Author

@svlandeg I don't mean to be rude, but is there any progress on getting this in?
I haven't been able to find any docs talking about a release cycle, or anything else of sort.

@svlandeg
Copy link
Member

Hi @arielf212, thanks for your contribution adding this feature and an appropriate test! 🙏

We're currently working through a bit of a backlog of PRs, so unfortunately it may take a little while to get this properly reviewed by the team. We've put it on our internal board though, so we'll let you know here once we've been able to go through it in detail. Thanks for your patience!

@svlandeg
Copy link
Member

In the meantime, what would be helpful is if you can update the test suite to test all new lines of code, so that the coverage of the code base is back to 100% and the CI goes green again 🙏

@a3ng7n
Copy link

a3ng7n commented May 8, 2025

can outside folks contribute to this pr? I'd be happy to take a stab at writing the rest of the tests if that helps

Copy link

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

@arielf212, thanks for working on this!

We definitely need to cover this feature with more tests..

I found a couple of issues:

  • par: Union[int, str, None] = None (Type not yet supported: <class 'NoneType'>)
  • par: Union[int, float] (shows [default: None] [required] in help)

@a3ng7n, I think it would be nice if you could help with tests.
You can fork https://github.com/arielf212/typer/tree/feature/union (uncheck the "Copy the master branch only" checkbox), make changes and open PR to https://github.com/arielf212/typer/tree/feature/union repository. @arielf212 would be able to review and merge it to their branch.
Alternatively, you can write tests in your fork and send us the link to your branch. We will review and cherry-pick your commits

Comment on lines +62 to +63
def opt(id_or_name: Union[int, str]):
if isinstance(id_or_name, int):

Choose a reason for hiding this comment

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

Tests should be simpler.

    @app.command()
    def opt(id_or_name: Union[float, int, str]):
        print(f"{id_or_name} ({type(id_or_name)})")

Something like this

main_type = types[0]
origin = get_origin(main_type)
if len(types) == 1:
(main_type,) = types

Choose a reason for hiding this comment

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

IMO, main_type = types[0] is clearer.

Also, comment above should be updated (Handle SomeType | None and Optional[SomeType])

[item.value for item in annotation],
case_sensitive=parameter_info.case_sensitive,
)
elif get_origin(annotation) is not None and is_union(get_origin(annotation)):

Choose a reason for hiding this comment

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

Suggested change
elif get_origin(annotation) is not None and is_union(get_origin(annotation)):
elif is_union(get_origin(annotation)):

Seems that this should work

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 20, 2025

This comment was marked as resolved.

@github-actions github-actions bot removed conflicts Automatically generated when a PR has a merge conflict waiting labels Sep 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants