-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add support for os.PathLike #37
Conversation
Codecov ReportBase: 99.19% // Head: 99.19% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #37 +/- ##
=======================================
Coverage 99.19% 99.19%
=======================================
Files 21 21
Lines 1620 1624 +4
=======================================
+ Hits 1607 1611 +4
Misses 13 13
Flags with carried forward coverage won't be shown. Click here to find out more.
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
It can get a little more complicated than this if you want to create a custom Path. e.g., def test_custom_pathlike():
class CustomPath(pathlib.PurePosixPath):
def __new__(cls, *args: Union[str, os.PathLike]) -> "CustomPath":
return super().__new__(cls, *args)
def main(x: CustomPath) -> CustomPath:
return x
assert tyro.cli(main, args=["--x", "/dev/null"]) == CustomPath("/dev/null") There are a couple of edge cases:
|
|
class PurePath(object):
def __new__(cls, *args):
if cls is PurePath:
cls = PureWindowsPath if os.name == 'nt' else PurePosixPath
return cls._from_parts(args) So depending on the platform it'll actually create a different object. It doesn't utilize The problem here is actually around creating the instantiator: Lines 141 to 170 in 9269b6f
Tyro needs to take into account not only signatures that are
|
It seems this could be solved with something as simple as (logic could be made simpler, just a POC):
if i == 0 and not (
(
get_origin(param.annotation) is Union
and str in get_args(param.annotation)
)
or param.annotation in (str, inspect.Parameter.empty)
):
raise UnsupportedTypeAnnotationError(
f"Expected {typ} to be an `(arg: str) -> T` type converter, but"
f" got {signature}."
) |
Addressing #36!
Ideally this could be implemented using #30, but that probably won't be ready for a while. (or ever)