Skip to content

✨ Allow annotated parsing with a subclass of Path #1183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

emfdavid
Copy link

Please allow using custom Path subclasses such as UPath

Here is an example

import logging
from pathlib import Path
from typing import Annotated, Optional

import typer
from upath import UPath
app = typer.Typer()

@app.command()
def path_func(arg: Annotated[UPath, typer.Argument(parser=UPath)]):
    logger.info(f"got arg: '{arg}'; type: {type(arg)}")

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    app()

But as you can see in my debugger, the parsed value will get converted to a Path by the param_path_convertor.

Screenshot 2025-03-20 at 2 18 16 PM

Allowing that function to return anything that is already a Path (or maybe a PurePath?) would fix this issue, though, it maybe a problem for other types as well?

I can hack around this by setting the annotated type to None but using UPath as a parser, however that does not seem to be documented behavior or good practice.

@app.command()
def path_func(arg: Annotated[None, typer.Argument(parser=UPath)]):
    logger.info(f"got arg: '{arg}'; type: {type(arg)}")

@svlandeg svlandeg added the feature New feature, enhancement or request label Mar 21, 2025
@svlandeg svlandeg changed the title Allow annotated parsing with path subclasses ✨ Allow annotated parsing with a subclass of Path Mar 21, 2025
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

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

Hi, thanks for the contribution and feature request!

What you're proposing makes sense to me, and at a first glance the code looks good. Surprisingly, some of the CI tests do fail. Do you have time to look into those?

@emfdavid
Copy link
Author

Well - that was a rabbit hole.
Let's see if my hack works for older versions of python... if not can we just skip this test for older versions?

@emfdavid emfdavid force-pushed the support_path_subclasses branch from 7a8257c to ae9ddd5 Compare March 21, 2025 21:28
@emfdavid emfdavid requested a review from svlandeg March 21, 2025 21:35
@svlandeg svlandeg removed their request for review March 24, 2025 10:57
@emfdavid
Copy link
Author

@svlandeg what happens now?

@emfdavid emfdavid force-pushed the support_path_subclasses branch from f367808 to 4d78ba6 Compare March 25, 2025 13:59
@emfdavid emfdavid requested a review from svlandeg March 26, 2025 14:00
@svlandeg svlandeg removed their request for review March 26, 2025 16:44
@svlandeg
Copy link
Member

@svlandeg what happens now?

@emfdavid: we've put this on our internal reviewing board, but we're currently facing quite a bit of a backlog in PRs (also on fastapi etc), so it may take a while before we make a final decision. We'll keep you up-to-date here!

@emfdavid
Copy link
Author

@emfdavid: we've put this on our internal reviewing board, but we're currently facing quite a bit of a backlog in PRs (also on fastapi etc), so it may take a while before we make a final decision. We'll keep you up-to-date here!

Thank you for the update - I have monkey patched my applications for now.

@emfdavid emfdavid force-pushed the support_path_subclasses branch from 4d78ba6 to 40c6e1e Compare March 28, 2025 00:19
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.

2 participants