-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[FEATURE/DISCUSSION] Representations of TG objects #2491
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
Comments
But this just returns a number, thats not helpful at all when I want to know the type of the object. Did you mean type?
I like that idea, good to read while making clear its not a dict. And we can just let it fall back to repr, I would be fine with that.
no. its not. |
No, I mean that there are some cases like |
oh, I see. They should just use an if clause then smh |
Just a quick thought: v14 will drop py3.6, which means we can start using dataclasses at some point. dataclasses come with built-in support to print objects as
Haven't thought yet about which options I like best, just putting this here for reference Edit: py3.9 reaches EOL ~October 2025 |
Another idea: Instead of waiting for py3.10 to happen, we could just borrow the code from https://github.com/python/cpython/blob/3.10/Lib/pprint.py. See also python/cpython#24389. If we do this, we should double check how this works out with licenses, how we need to achknowledge python etc |
so you want to override pprint in our library? Or how would you borrow it? |
@Poolitzer Not really override, rather use a vendored version instead. But for it's just an idea. Noam also pointed out the pprint36 library. Maybe it's worth asking if they want to update to 3.10 |
okay. but vendoring it means the linebreaks and indention for dataclasses will only be shown whent he user uses pprint, and not print (which means one import more). So vendoring pprint or using pprint36 if they update to 3.10 means the dataclass representation will look a bit nicer automatically, if the user uses pprint. I would argue thats not on us. The user can decide to do that, or to use 3.10. I think prints are often used as fast debug statements, so its not important that it looks perfect, it should just show everything needed. So I will say that converting the objects to dataclasses is enough. We can add a note in the docs somewhere saying how to make the representation look even better. |
I thinks we're talking past each other. The original intention of this issue was to improve the Now my ideas was that if we convert everything to data classes anyway, we could leverage that for this issue. Vendoring Converting to dataclass ofc has the positive side effect that in py 3.10+ the built in pprint works well on TO. But the main point of this issue is to provide helpful string representations of TO independent of how TO is implemented |
Okay. But the converting of all objects to data classes already improves the representation, no? |
Right, I didn't put that correctly. Still, how exactly we get Having a method |
alright, I can agree to that. I see that you switched stages, so nothing imminent anyway, and we can discuss the implementation detail once we get to the stage |
Closed by #3234 |
This came up in church and my comment on it is too lengthy to post a question in dev chat, so here we go. Also this is not urgent in any way …
Current state
Currently
str(tg_objec)
will givestr(tg_object.to_dict())
andrepr(tg_object)
will give somthing like<TGObject at …>
.The first one can be confusing for newbies as they might thing that
tg_object
is a dictionary and they will start using subscription instead of dot-notation and so on …The second one has
id(tg_object)
.Desired behavior
In view of above description, I think that it would be desirable to have a more helpful representation of TG Objects. We should keep in mind that
__repr__
goal is to be unambiguous__str__
goal is to be readablestr()
calls__repr__
, if__str__
is not definedI think in our case "unambiguous" and "readable" largely collide. I.e. a representation of the form
ClassName(argname=value, …)
is both unambiguous and good to read. So this is what I propose we should do in detail:Proposal:
TO.pprint(linebreak: bool=False, indent: int=2)
. This will returnClassName(argname=value, …)
. Whenlinebreak
isTrue
it will givef'ClassName(\n{indent*" "}argname=value,\n{indent*" "}…)'
, so making it something like pythons built-inpprint
for dicts. I think this should be rather straight forward. only thing that could make trouble is attributes with list-values …TgO.__repr__
useTgO.pprint
. Here we could argue, if you want to skip arguments that are not in_id_attrs
, as_id_attrs
alone can be argumed to be unambiguous. But I think we're better of just including everythingTgO.__str__
, which would then fallback toTgO.__repr__
Questions:
The text was updated successfully, but these errors were encountered: