[issue45331] Can create enum of ranges, cannot create range enum. Range should be subclassable... or EnumMeta.__new__ should be smarter.

2021-09-30 Thread Sam Bishop


New submission from Sam Bishop :

Range types are perfectly valid as values in an enum, like so.

class EnumOfRanges(Enum):
ZERO = range(0, 0)
RANGE_A = range(1, 11)
RANGE_B = range(11, 26)


However unlike the other base types , 'int', 'str', 'float', etc. You cannot 
create a "range enum" 

class RangeEnum(range, Enum):
ZERO = range(0, 0)
RANGE_A = range(1, 11)
RANGE_B = range(11, 26)

produces `TypeError: type 'range' is not an acceptable base type` when you try 
and import `RangeEnum`.

The current documentation for `enum` implicitly says this should work by not 
mentioning anything special here 
https://docs.python.org/3/library/enum.html#others

It also allows the use of range objects as value types, another implicit 
suggestion that we should be able to restrict an enum class to just range 
values like we can for other builtin class types.

Also to keep this a concise issue:
- Yes I'm aware not all of the base classes can be subclassed.
- Yes I know I that there are good reasons bool should not be subclassable.

So I'd like to suggest one of three things should be done to improve the 
situation:

A: Solve https://bugs.python.org/issue17279 and by documenting the special base 
class objects that cannot be subclassed and reference this in the documentation 
for Enums.

B: Make a decision as to which base class objects we should be able to 
subclass, and then improve their C implementations to allow subclassing. (It's 
also probably worth documenting the final list of special objects and solving 
https://bugs.python.org/issue17279 should this approach be selected.) 

C: The __new__ method on EnumMeta should be made smarter so that it either 
emits a more useful warning (I had to head to the CPython source code to work 
out what the error `TypeError: type 'range' is not an acceptable base type` 
meant) or somehow being more smart about how it handles the special classes 
which can't cannot be subclassed allowing them to be used anyway.  which again 
sort of involves solving https://bugs.python.org/issue17279, and in the case 
that its just made magically smarter, I'll admit could confuse some people as 
to why "Enum" is special and can subclass these but their own code can't just 
do `class MyRange(range):` 

Regardless of the outcome, it would be good to fill in this pitfall one way or 
the other for the sake of future developers, I'm a reasonably experienced 
Python developer and it caught me by surprise I'm likely not the first and 
probably wont be the last if the behaviour remains as it currently is.

--
components: Interpreter Core
messages: 402960
nosy: techdragon
priority: normal
severity: normal
status: open
title: Can create enum of ranges, cannot create range enum. Range should be 
subclassable... or EnumMeta.__new__ should be smarter.
type: enhancement
versions: Python 3.9

___
Python tracker 
<https://bugs.python.org/issue45331>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34498] Python 3.7 breaks on singledispatch_function.register(pseudo_type), which Python 3.6 accepted

2018-10-03 Thread Sam Bishop


Sam Bishop  added the comment:

Would the enhancements to resolve this, by making singledispatch accept more 
things, also resolve the AssertionError from functools.singledispatch when 
passing it custom types, or should I raise this as a separate issue? 


from typing import NewType, List
from functools import singledispatch

@singledispatch
def fun(arg, verbose=False):
if verbose:
print("Let me just say,", end=" ")
print(arg)

MyType = NewType('MyType', List[int])

@fun.register
def _(arg: MyType , verbose=False):
if verbose:
print("Strength in numbers, eh?", end=" ")
print(arg)

---
AssertionErrorTraceback (most recent call last)
 in 
  5 
  6 @fun.register
> 7 def _(arg: MyType , verbose=False):
  8 if verbose:
  9 print("Strength in numbers, eh?", end=" ")

~/.pyenv/versions/3.7.0/lib/python3.7/functools.py in register(cls, func)
809 argname, cls = next(iter(get_type_hints(func).items()))
810 assert isinstance(cls, type), (
--> 811 f"Invalid annotation for {argname!r}. {cls!r} is not a 
class."
812 )
813 registry[cls] = func

AssertionError: Invalid annotation for 'arg'. .new_type at 0x10fcd7730> is not a class.

--
nosy: +techdragon

___
Python tracker 
<https://bugs.python.org/issue34498>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22790] __qualname__ missing from dir(__class__) during class initialisation

2014-11-04 Thread Sam Bishop

New submission from Sam Bishop:

The output of performing dir(__class__) during a class' __init__ method, 
seems to be lacking the new '__qualname__' attribute in python 3.

This rough test can be pasted right into the python 3.4 REPL to see the issue.
Tested on 64bit python 3.4 running on OSX 10.9
 

class Foo:
class Bar(object):
keywords = dict()
def __init__(self, **kwargs):
print(dir(__class__))
print(''.join(('str(__class__.__qualname__) = ', 
str(__class__.__qualname__
print(''.join((
'Is __qualname__ in the output of the dir() function? : ',
str(('__qualname__' in dir(__class__))
self.keywords = kwargs


test = Foo.Bar(see='We are missing something here')




--
components: Interpreter Core
messages: 230591
nosy: techdragon
priority: normal
severity: normal
status: open
title: __qualname__ missing from dir(__class__) during class initialisation
type: behavior
versions: Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22790
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22790] some class attributes missing from dir(Class)

2014-11-04 Thread Sam Bishop

Sam Bishop added the comment:

I specified 'during class initialisation' because that was the only case I 
confirmed.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22790
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com