Skip to content
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

Document how to use Show instance for formatting #129

Open
konsumlamm opened this issue Aug 14, 2023 · 3 comments
Open

Document how to use Show instance for formatting #129

konsumlamm opened this issue Aug 14, 2023 · 3 comments

Comments

@konsumlamm
Copy link
Contributor

One can use :s to use the Show instance for formatting, but I don't remember how I found that out, since it isn't documented anywhere as far as I can tell. It would also be nice to use the Show instance by default (currently, trying to format a custom type gives a big "no instance" error), if that's possible.

@guibou
Copy link
Owner

guibou commented Nov 8, 2023

Hello @konsumlamm, thank you for this issue.

AFAIK, :s is implicitly documented by the python documentation (after all, it means "format as string"). But you are right, more documentation would be great. I need to find the time to do that (or find the time to approve super nice MR as you did recently, thank you).

Regarding having Show by default, I don't really know what to do. PyF uses this complex type class PyFClassify in order to classify what is the default formatting mode for something. And the error message you got is because there is no PyFClassify instance for your type, and hence no FormatAny2 (Yes, naming is difficult).

It may be possible to fix that using more overlapping instances, but everytime I tried something with overlapping instances, I ended in an aweful situation.

@guibou
Copy link
Owner

guibou commented Nov 8, 2023

And actually, in python, and maybe that's a good thing, formatting any object with s fails if the object does not have an __str__ instance. Well, I'm unsure about the correct semantic, but:

>>> f"{10:s}"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 's' for object of type 'int'

edit: actually the discussion is about what to do when there is no type specifier, not what to do when the s is there.

@guibou
Copy link
Owner

guibou commented Nov 8, 2023

Edit:

Actually, I may be able to do something, at the cost of a code simplification, a little bit less magic and more power.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants