Skip to content

Conversation

emfdavid
Copy link
Contributor

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
Contributor 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
Contributor 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
Contributor 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
@delsner
Copy link

delsner commented Aug 19, 2025

@svlandeg any idea when this will land? Thanks!

@svlandeg
Copy link
Member

@delsner & @emfdavid : I've been engaged on other projects for the last few months, but I'll try and review this in detail this week! Thanks for your patience 🙏

@svlandeg svlandeg self-assigned this Aug 19, 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.

This is a nice fix that supports subclassing Path cleanly. I verified that the test fails on master and otherwise everything looks good to me.

I'll leave this for Tiangolo for a final check.

Thanks again for your work on this, @emfdavid!

@svlandeg svlandeg removed their assignment Aug 20, 2025
Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Awesome, makes sense, thank you @emfdavid! 🚀

I can see how figuring out Python < 3.12 was probably hard with the _flavor stuff. 😅

Thanks @svlandeg for the review and help! 🎉


This will be available in Typer 0.17.3 in the next minutes. 😎

@tiangolo tiangolo merged commit 0b8fa09 into fastapi:master Aug 30, 2025
24 checks passed
@emfdavid emfdavid deleted the support_path_subclasses branch September 9, 2025 16:07
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