-
Notifications
You must be signed in to change notification settings - Fork 121
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
Implicit Path() to str conversion fails in 2.2.1 #934
Comments
Hi @titu1994, It's now possible to use @dataclass
class CallbackParams2:
dirpath: Optional[Path] = None Would that work for your use-case? Another workaround could be to convert to a string explicitly: cfg.dirpath = str(logdir / Path('logs')) |
While sure explicitly casting dataclasses to Path instead of str would work, in our case we have some 150+ configs (public, internal, external users etc), and over 50+ dataclasses which somewhere or other depend on this automatic conversion. It would be nearly impossible to safely upgrade all of them, and intractable to do it even for just the public ones. It would be more useful to support implicit casting - because Python in general makes no major distinction between the two for files and treats Path() as a string implicitly as needed. More interestingly, semantically we have the dataclass argument as type str - and path can be casted to str via str(path) so the auto conversion should work out of the box even if Path() is passed during assignment. |
Implicit Path() to string conversion seems common enough that we should maintain support. |
Thank you ! |
Describe the bug
When assigning a value to a structured dataclass, attempting to convert a Path() object implicitly to str fails -
To Reproduce
Python file :
Output :
Expected behavior
Additional context
The text was updated successfully, but these errors were encountered: