python / cpython

The Python programming language
https://www.python.org
Other
63.15k stars 30.24k forks source link

issubclass() should accept iterables in 2nd arg #61359

Closed 918f67d7-4fec-4a8d-93e3-6530aeb1e57e closed 3 years ago

918f67d7-4fec-4a8d-93e3-6530aeb1e57e commented 11 years ago
BPO 17157
Nosy @terryjreedy, @mdickinson, @iritkatriel

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: ```python assignee = None closed_at = created_at = labels = ['interpreter-core', 'type-feature'] title = 'issubclass() should accept iterables in 2nd arg' updated_at = user = 'https://bugs.python.org/RamchandraApte' ``` bugs.python.org fields: ```python activity = actor = 'terry.reedy' assignee = 'none' closed = True closed_date = closer = 'iritkatriel' components = ['Interpreter Core'] creation = creator = 'Ramchandra Apte' dependencies = [] files = [] hgrepos = [] issue_num = 17157 keywords = [] message_count = 7.0 messages = ['181670', '181672', '181714', '181730', '181737', '381189', '381209'] nosy_count = 5.0 nosy_names = ['terry.reedy', 'mark.dickinson', 'Ramchandra Apte', 'franck', 'iritkatriel'] pr_nums = [] priority = 'normal' resolution = 'rejected' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue17157' versions = ['Python 3.4'] ```

918f67d7-4fec-4a8d-93e3-6530aeb1e57e commented 11 years ago

kushou pointed this out on #python-dev

mdickinson commented 11 years ago

What's the use case for this? issubclass already accept tuples, just like isinstance:

>>> issubclass(bool, (int, float))
True
terryjreedy commented 11 years ago

Given isxxx(src, target_s), the proposal would seem to be to change the internal test "type(target_s) is tuple" to "hasattr(type(targets), '\_iter'). This depends on metaclasses not having .__iter methods, just as type does not. However, a subclass of type that added .__iter__, for instance to iterate through all its instances (classes defined with that metaclass), would break that test. So the proposal needs a better test, that cannot become ambiguous, to be practical.

A virtue of the 'class or tuple of classes' interface is that a tuple instance is clearly not a class in itself, so there is no possible ambiguity.

It is a positive feature that isinstance and issubclass have the same signature: both or neither should be changed. The use of tuple for multiple items in 'item or items' interfaces is historical and also used elsewhere, as in exception clauses.

The meaning of except target_exception_s [as name]: body is if issubclass(raised_exception, target_exception_s) or isinstance(raised_exception, target_exception_s): [name = raised_exception] body

So to remain consistent, I think changing exception statements to allow iterables of exceptions should also be part of the proposal.

There might be something else that I am forgetting about at the moment.

While iterables might be used if Python were being written fresh today and the ambiguity problem were solved, I agree that more than esthetic satisfaction is needed to make a change.

918f67d7-4fec-4a8d-93e3-6530aeb1e57e commented 11 years ago

Just so you know, I'm neutral on this idea. I think it should at least accept sequences though.

b4f50f8b-2949-4b36-8075-02292f7b2a6a commented 11 years ago

I am neutral on this too, it just felt odd and this is why the question raised.

Yesterday I tried to add sequences and iterables (only to issubclass but indeed it would need better testing and all) and came to a problem with sequences. The current implementation authorizes (A, (B, (C, D))) (why?) so issubclass is recursive, but if we add sequences, they could create infinite recursions if seq[0] == seq (it's the case for strings for example, where 'a' is still a sequence and 'a'[0] == 'a').

So I don't know if it's really interesting. Anyone can use tuple() on an iterable to build its tuple value, though the value is built in memory. It basically just felt odd to take only tuples but I don't know.

iritkatriel commented 3 years ago

Good discussion. There seems to be agreement that this should not be done.

terryjreedy commented 3 years ago

except ... is another instance of (exception) class or tuple of classes.