Navigation Menu

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

Move non-collections-related ABCs out of collections.abc #69823

Closed
brettcannon opened this issue Nov 16, 2015 · 14 comments
Closed

Move non-collections-related ABCs out of collections.abc #69823

brettcannon opened this issue Nov 16, 2015 · 14 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@brettcannon
Copy link
Member

BPO 25637
Nosy @gvanrossum, @brettcannon, @rhettinger, @terryjreedy, @ncoghlan, @ezio-melotti, @vadmium, @1st1

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2016-01-05.21:14:59.481>
created_at = <Date 2015-11-16.19:21:22.704>
labels = ['type-feature', 'library']
title = 'Move non-collections-related ABCs out of collections.abc'
updated_at = <Date 2016-01-05.21:14:59.480>
user = 'https://github.com/brettcannon'

bugs.python.org fields:

activity = <Date 2016-01-05.21:14:59.480>
actor = 'brett.cannon'
assignee = 'none'
closed = True
closed_date = <Date 2016-01-05.21:14:59.481>
closer = 'brett.cannon'
components = ['Library (Lib)']
creation = <Date 2015-11-16.19:21:22.704>
creator = 'brett.cannon'
dependencies = []
files = []
hgrepos = []
issue_num = 25637
keywords = []
message_count = 14.0
messages = ['254744', '254747', '254750', '254861', '255013', '257204', '257214', '257274', '257375', '257384', '257547', '257552', '257553', '257554']
nosy_count = 9.0
nosy_names = ['gvanrossum', 'brett.cannon', 'rhettinger', 'terry.reedy', 'ncoghlan', 'stutzbach', 'ezio.melotti', 'martin.panter', 'yselivanov']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = 'test needed'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue25637'
versions = ['Python 3.6']

@brettcannon
Copy link
Member Author

In discussing adding a context manager ABC in issue bpo-25609, the idea of creating a new module for ABCs that is more analogous to the types modules came up. Currently all ABCs that are generic in the stdlib and don't belong to a specific module end up in collections.abc, e.g. the Awaitable ABC has nothing to do with data structures and yet it's in a module in the collections package.

What we could do is create an interfaces module which contains all the ABCs that are not specific to a package. This would mean moving the ABCs out of collections.abc that have nothing to do with collections such as things for async, etc. The real question, though, is how to decide what belongs where, e.g. is Iterable/Iterator a collection, and if so then what about Generator?

The other option is that instead of introducing a generic interfaces module we simply create appropriate new modules to house the thematic ABCs and avoid a dumping ground scenario, e.g. some new module for the async stuff instead of just dumping them in an interfaces module. All the other ABCs could probably find existing homes, e.g. Callable could go into functools (although do we want to start a practice of having an abc submodule for all instances where ABCs exist and thus introduce functools.abc?).

@brettcannon brettcannon added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Nov 16, 2015
@1st1
Copy link
Member

1st1 commented Nov 16, 2015

We had this discussion when we were adding Awaitable, AsyncIterable etc. I suggested to simply put them in the top-level 'abc' module (where ABCMeta is defined).

@brettcannon
Copy link
Member Author

I guess is the objection of putting concrete ABCs in the abc module is that the abc module contains the actual code to implement ABCs and not ABCs themselves. Personally I would be fine with a submodule within abc like abc.interfaces.

Personally I'm starting to warm to the idea of sticking ABCs in proper packages as e.g. functools.abc for Callable; namespaces are one honking great idea after all. :)

@1st1
Copy link
Member

1st1 commented Nov 18, 2015

I guess is the objection of putting concrete ABCs in the abc module is that the abc module contains the actual code to implement ABCs and not ABCs themselves.

I think it's a rather weak objection. Having some basic (and fundamental!) ABCs defined along with ABCMeta won't hurt.

Personally I would be fine with a submodule within abc like abc.interfaces.

Personally I'm starting to warm to the idea of sticking ABCs in proper packages as e.g. functools.abc for Callable; namespaces are one honking great idea after all. :)

It will be hard to find proper namespace for things like Hashable and Awaitable. That's why I like the idea of putting them in the top-level 'abc' module.

@terryjreedy
Copy link
Member

I would not like to have, say, 20 namespaces, some with just one ABD, such as functools.abc.Callable. I might have preferred abc.collections to collections.abc, etc.

@brettcannon
Copy link
Member Author

I brought this topic up on python-ideas and both Nick Coghlan and MA Lemburg suggested the abc module like Yury did. So the proposal becomes to put syntax-related ABCs into the abc module and domain-specific ones in their respective stdlib modules.

Nick suggested the following ABCs get pulled out of collections.abc and put into the abc module:

Callable - function calls
Iterable - for loops, comprehensions
Iterator - for loops, comprehensions
Generator - generators, generator expressions
Awaitable - await expressions
Coroutine - async def
AsyncIterable - async for
AsyncIterator  - async for

@terryjreedy
Copy link
Member

That looks like a good list to me.

@ncoghlan
Copy link
Contributor

ncoghlan commented Jan 1, 2016

From a documentation perspective, I'd suggest breaking the ABC module documentation at https://docs.python.org/3/library/abc.html into three subsections:

29.7.1 Defining Abstract Base Classes

This would include the existing docs for ABC, ABCMeta, abstractmethod and get_cache_token.

29.7.2 Syntactic ABCs

New section containing the documentation for the ABCs relocated from collections.abc

29.7.3 Deprecated decorator API

The docs for abstractclassmethod, abstractstaticmethod and abstractproperty can be moved out of the main ABC definition docs, since the only reason to use them now is for single source Python 2/3 compatibility, or compatibility with Python 3.2.

@vadmium
Copy link
Member

vadmium commented Jan 2, 2016

Did anyone consider moving these near the “types” module, either directly inside, or as a “types.abc” submodule? In my mind, these ABCs would fit reasonably well there. They are related to built-in types, but do not have built-in names.

@1st1
Copy link
Member

1st1 commented Jan 2, 2016

Did anyone consider moving these near the “types” module, either directly inside, or as a “types.abc” submodule? In my mind, these ABCs would fit reasonably well there. They are related to built-in types, but do not have built-in names.

Big -1. Let's avoid nesting 'abc' modules throughout the standard library. One of the good practices in Python is to import modules, not classes from modules. With 'types.abc' I'll have to import 'from types import abc', and then, in the code, I won't be so sure which 'abc' is it -- 'abc', 'types.abc', 'collections.abc' etc.

Putting ABCs in the 'types' module directly also doesn't feel right and can confuse users. 'types' already contains things like 'GeneratorType', 'CoroutineType' etc, which are actual types (not ABCs).

@gvanrossum
Copy link
Member

Honestly I think this such a slippery slope that I prefer keeping them in collections.abc. The main reason that we have Iterable and Iterator is that they are closely related to real collections (e.g. Sequence, Set, Mapping). And generators are related to iterators. And so on. To me the force that wants to keep abc.py purely about *implementing* ABCs is stronger than the desire to have only things that really are ABCs for collection types in collections.abc.

The types.py module is a bunch of crap that we can't get rid of yet but it should not be used for anything new.

@brettcannon
Copy link
Member Author

So leave Callable and Coroutine there and if I add a context manager ABC put it in collections.abc as well? Or should we put Callable and Coroutine in functools and a context manager one in contextlib (or Coroutine in asyncio)?

@gvanrossum
Copy link
Member

I think ContextManager should be in contextlib. I don't want to put
anything in functools. Coroutine doesn't belong in asyncio, it's more
fundamental (closely related to Generator).

@brettcannon
Copy link
Member Author

Thanks for the input everyone!

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

6 participants