Add Formatter.pop(type)#4561
Conversation
- for_type(cls) nondestructively queries the current formatter (previously disallowed) - for_type(cls, None) clears the current formatter (previously queried current)
|
I don't know how many people are using this API, but if we were concerned about backwards compatibility, but didn't want the rather unintuitive reversal, we could |
|
Why not create a new set of On Wed, Nov 20, 2013 at 11:41 AM, Thomas Kluyver
Brian E. Granger |
|
Actually, yes, I think an explicit |
|
What is the benefit of |
|
Does nobody else find it surprising that registering None as a formatter via |
|
Mildly surprising, perhaps, but not enough to make me keen on an API-breaking change. Especially because the failure mode if people are using that to check the current formatter is quite annoying - it will still return the value they expect, but then stop using that function, with no warning. To change my position again: |
|
I should also note that the changed behavior was entirely undocumented, and a search on GitHub found no evidence of users of the undocumented behavior. |
|
I am fine with |
|
FWIW: the API originates in the upstream http://dev.pocoo.org/hg/sandbox/file/98ce1ce17c7c/pretty/pretty.py#l634 |
|
Thanks, @rkern, makes more sense now. Any preferences between:
|
|
I have a preference for adding It's too bad that I stuck you guys with separate methods for registering by type and type name. In my more recent incarnations of this pattern, I have put both in the same method. That makes it less of a load on the API to add removal and query methods. https://github.com/enthought/apptools/blob/master/apptools/type_registry/type_registry.py |
|
Just a note, please add a |
adds - formatter.lookup(instance) - formatter.lookup_by_type(type_or_type_string) - formatter.pop(type_or_type_string) changes: - support 'module.name' in formatter.for_type
|
Okay, I copied some of the logic from apptools, thanks @rkern. The current behavior:
The inner data structures are unchanged, so people reaching in and calling pop already should be unaffected. @ivanov I will make sure to add the what's new notes, and also propagate usage of this API in the few places we would have used them if they existed. |
|
what's new added, and pop usage propagated. Should be ready for another review pass. |
There was a problem hiding this comment.
The docstring says it returns a string, but it actually returns a tuple.
There was a problem hiding this comment.
Thanks, sorry - that's my bad copying, I will take another run at the docstrings to find any other things that don't map.
|
The lookup, pop behavior is directly from apptools, but I was considering making the following changes:
Any objections to those? |
- pop takes a default value (like dict) - support `type in Formatter` - allow lookup by type string (for consistency)
|
I was mainly thinking of wanting to register a formatter for a not-yet-imported type, but only if one doesn't already exist: if 'not_yet_imported.type' not in formatter:
formatter.for_type('not_yet_imported.type', my_formatter)Also, it was just for consistency, since I kept trying to use it this way in the tests to verify that things were working properly, and all of the other API calls support it.
yes, definitely. I only meant to use lookup_for_type to perform the check, the return is still bool. The code should reflect these changes now. |
|
The docstrings could use another pass (for instance, rationalizing the type description of |
|
I didn't saw anything specific to say, but this is a place of the code where I'm not confortable. |
Copied some of the logic from apptools (thanks @rkern). The current behavior: - `lookup(instance)` - return appropriate callback or a given object - `lookup_by_type(type_or_str)` - return appropriate callback for a given type or `'mod.name'` type string - `for_type(type_or_str)` - behaves the same, only adding support for type strings for consistency - `pop(type_or_str[, default])` - remove a type (by type or string) - `typ_or_str in Formatter` checks whether a type is registered, checking with `lookup_by_type` The inner structures remain unchanged.
Copied some of the logic from apptools (thanks @rkern). The current behavior: - `lookup(instance)` - return appropriate callback or a given object - `lookup_by_type(type_or_str)` - return appropriate callback for a given type or `'mod.name'` type string - `for_type(type_or_str)` - behaves the same, only adding support for type strings for consistency - `pop(type_or_str[, default])` - remove a type (by type or string) - `typ_or_str in Formatter` checks whether a type is registered, checking with `lookup_by_type` The inner structures remain unchanged.
Note: this commit also drop comment for latex_formatter.for_type, I think after ipython/ipython#4561 this method is well documented.
Note: this commit also drop comment for latex_formatter.for_type, I think after ipython/ipython#4561 this method is well documented.
I copied some of the logic from apptools (thanks @rkern).
The current behavior:
lookup(instance)- return appropriate callback or a given objectlookup_by_type(type_or_str)- return appropriate callback for a given type or'mod.name'type stringfor_type(type_or_str)- behaves the same, only adding support for type strings for consistencypop(type_or_str[, default])- remove a type (by type or string)typ_or_str in Formatterchecks whether a type is registered, checking withlookup_by_typeThe inner structures remain unchanged.