Issue10868
Created on 2011-01-08 23:32 by kerio, last changed 2022-04-11 14:57 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| abcmeta_register_deco.diff | kerio, 2011-01-08 23:32 | proposed patch | ||
| abcmeta_register_deco_with_docs.diff | kerio, 2011-01-09 00:05 | proposed patch with docs and corrections | ||
| abcmeta_register_v3.diff | eric.araujo, 2011-01-09 00:19 | |||
| abcmeta_register_v4.diff | kerio, 2011-01-10 17:51 | |||
| abcmeta_register_v4_plus_tests.diff | kerio, 2011-01-10 19:25 | |||
| Messages (18) | |||
|---|---|---|---|
| msg125804 - (view) | Author: Edoardo Spadolini (kerio) | Date: 2011-01-08 23:32 | |
If we make ABCMeta.register() return the registered class, like atexit.register() does for the registered function, we can then use it as a decorator:
Now:
class Foo:
...
ABarC.register(Foo)
With this change:
@ABarC.register
class Foo:
...
The only problem this would cause is in code that relies on ABCMeta.register() to return None; I can't think of a reason anyone would rely on this, but...
|
|||
| msg125806 - (view) | Author: Antoine Pitrou (pitrou) * ![]() |
Date: 2011-01-08 23:33 | |
Looks like a good idea to me. |
|||
| msg125807 - (view) | Author: Éric Araujo (eric.araujo) * ![]() |
Date: 2011-01-08 23:41 | |
Thanks for the report and patch. Some comments: + """Register a virtual subclass of an ABC. Returns the said subclass.""" Should be “Return”, for consistency. + return subclass # For usage as a decorator I’d move that to the docstring. Can you edit the docs (remember to use the versionchanged directive) too? |
|||
| msg125810 - (view) | Author: Edoardo Spadolini (kerio) | Date: 2011-01-09 00:05 | |
Ok, edited the docs too; hope I did everything right... |
|||
| msg125814 - (view) | Author: Éric Araujo (eric.araujo) * ![]() |
Date: 2011-01-09 00:19 | |
Edited your patch to fix some nits. If there is no opposition, I’ll commit this to py3k when 3.2 is out. |
|||
| msg125816 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2011-01-09 00:30 | |
+1 |
|||
| msg125905 - (view) | Author: Daniel Urban (daniel.urban) * ![]() |
Date: 2011-01-10 17:36 | |
There is another return statement earlier in the ABCMeta.register method. The patch probably should also modify that. |
|||
| msg125907 - (view) | Author: Edoardo Spadolini (kerio) | Date: 2011-01-10 17:51 | |
Whoops, corrected that - should I add some more tests for that too? |
|||
| msg125909 - (view) | Author: Éric Araujo (eric.araujo) * ![]() |
Date: 2011-01-10 17:57 | |
You should, otherwise how would you prove it’s fixed? :) |
|||
| msg125912 - (view) | Author: Edoardo Spadolini (kerio) | Date: 2011-01-10 18:58 | |
Fair enough :) |
|||
| msg125915 - (view) | Author: Éric Araujo (eric.araujo) * ![]() |
Date: 2011-01-10 19:33 | |
- return # Already a subclass + return subclass # Already a subclass PEP 8 advises to put two spaces before inline comments, for readability. (There is no need to upload a new patch, I’ll change that before commit.) + self.assertIsInstahce(c, (A,)) This is fixed in the newest version of your patch. Running tests before uploading diffs catches such errors :) |
|||
| msg126003 - (view) | Author: Edoardo Spadolini (kerio) | Date: 2011-01-11 12:41 | |
Yeah, I should've waited for the test to finish, but come on, "it was just a small change" :( Now I know why you should always test everything at least, sorry about that :) |
|||
| msg129298 - (view) | Author: Éric Araujo (eric.araujo) * ![]() |
Date: 2011-02-24 18:18 | |
Committed to py3k as r88545. You’ll notice that I fixed the nesting of the versionchanged directive and that I changed my mind about “returns”. Thanks again! |
|||
| msg129300 - (view) | Author: Daniel Stutzbach (stutzbach) ![]() |
Date: 2011-02-24 18:37 | |
In what use-cases would you want to call MyABC.register() when defining a class instead of inheriting from MyABC? I always thought of the register() as hack to make it possible to support types written in C, which can't inherit from the ABC. |
|||
| msg129310 - (view) | Author: Éric Araujo (eric.araujo) * ![]() |
Date: 2011-02-24 21:20 | |
Someone may want to register with an ABC but not inherit methods or add a class to the mro. It’s always been allowed by the register method; the new decorator feature is just a very minor nicety on top of that. Edoardo, was your request motivated by a real use case where you didn’t want to inherit from the ABC? |
|||
| msg129521 - (view) | Author: Edoardo Spadolini (kerio) | Date: 2011-02-26 10:35 | |
Not really, but putting something in your inheritance lattice only to mark a class as providing an interface just doesn't seem right to me. |
|||
| msg129536 - (view) | Author: Daniel Urban (daniel.urban) * ![]() |
Date: 2011-02-26 12:32 | |
> In what use-cases would you want to call MyABC.register() when defining > a class instead of inheriting from MyABC? For example if you don't want to inherit a __dict__ for a tree-like data structure (see also issue11333). |
|||
| msg129571 - (view) | Author: Daniel Stutzbach (stutzbach) ![]() |
Date: 2011-02-26 16:32 | |
Sounds good to me. Thanks for the clarifications and satisfying my curiosity! :-) |
|||

