[Python-ideas] Re: len(Enum) should raise TypeError

2023-08-14 Thread outthere
In addition to this, there's nothing wrong with defining additional behavior on 
class objects. They're just objects. The pattern of class objects acting as 
containers of registered instances is used in many places, and it makes perfect 
sense here IMO.

On Tue, Apr 4, 2023, at 2:54 PM, Ethan Furman wrote:
> Enums are not like any other type in a multitude of ways: `__len__`, 
> `__contains__`, `__getitem__`, etc.  Special-casing 
> only `Enum`, `IntEnum`, `StrEnum`, and every other base enum (i.e. no 
> members), would be like special-casing empty 
> lists, empty dicts, empty tuples, etc.
>
> --
> ~Ethan~
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PHFBAFQJOSDAIY4LK3OOG7B3O3IO5HOO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-05 Thread Thomas Grainger
mypy does not detect this as a problem because EnumMeta has a `.__len__` method 
https://github.com/python/typeshed/blob/60939b00afede13feeec3cee6f6dfe6eb2df1593/stdlib/enum.pyi#L121

what would the type hints look like if len(Enum) failed but class Foo(Enum): 
pass len(Foo) succeeds?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RTAF57ZHKB2B75T3OJ73WBNT6JRUQF44/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-05 Thread Richard Hajek
> Can any Python linting tools (such as pylint) detect a potential
> problem with the code?

Hey,
I checked mypy, flake8, pylint, all did not see this as a problem. My native 
IDE ( PyCharm ) also saw no problem with it.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/W42HLCKELCO2OT6VVMK46EGYNGG7HY44/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-04 Thread Ethan Furman

On 4/1/23 07:45, Richard Hajek wrote:
> # Feature or enhancement
>
> `len(Enum)` has no useful meaning and should therefore raise the appropriate 
error
>
> # Pitch
>
> I cannot figure out any scenario, in which len(Enum) should not raise. Enum is a type and as any other types, such as 
`len(list)` or `len(dict)`, and should not give any meaningful result on len.


Enums are not like any other type in a multitude of ways: `__len__`, `__contains__`, `__getitem__`, etc.  Special-casing 
only `Enum`, `IntEnum`, `StrEnum`, and every other base enum (i.e. no members), would be like special-casing empty 
lists, empty dicts, empty tuples, etc.


--
~Ethan~
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YW6PLGLW4N6ZAZWR6ELED2ZIIPL3E4XQ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-04 Thread Jonathan Fine
Hi

Here's a similar example

$ python3
> Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from collections import Counter
> >>> cnt = Counter # Oops!
> >>> cnt.update('abcde')
> >>> cnt
> 
>

This is what happens without the typo.

>>> cnt = Counter()
> >>> cnt.update('abcde')
> >>> cnt
> Counter({'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1})
>

Here's a link to the source for Counter.update:

https://github.com/python/cpython/blob/3.11/Lib/collections/__init__.py#L658-L690

-- 
Jonathan
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UXD6EAUTFGLHPALMHVJQ53A5SG3DFRAF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-04 Thread James Addison via Python-ideas
Hi Richard,

On Tue, 4 Apr 2023 at 12:49, Richard Hajek  wrote:
>
> I encountered that Enum having a len hid a mistake on my part. If len(Enum) 
> raised, my mistake would be immediately apparent, however at the end of the 
> day, my mistake was easily found.

Can any Python linting tools (such as pylint) detect a potential
problem with the code?

Thanks,
James
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QGKXR35IWKPH44QFXKJNZCNFRNE6JJTX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-04 Thread Richard Hajek
> Yes, that is correct. Any subclass of Enum will also be a type, and
will have a length.

That is the correct and expected behavior, as described on 
https://docs.python.org/3/library/enum.html

> Yeah, and I'm of the opinion that it's not a problem for Enum to have
zero possible values, but to have the concept of values.

I encountered that Enum having a len hid a mistake on my part. If len(Enum) 
raised, my mistake would be immediately apparent, however at the end of the 
day, my mistake was easily found.

I am using this issue to mostly try how to contribute to Python and I do not 
have strong opinion on this issue either way. IMHO len(Enum) should raise, but 
idk if it is worth potential downstream breakages.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/EJWIJTZSRJEZIGUKZ6PUXOYZWLGXULSE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-04 Thread Richard Hajek
I believe that is the core question. In my use case it caused a bug because I 
accidentally typed "Enum" instead of "MyEnum" and the "len" worked, which is 
something I did not expect. However I do not know how prevalent this mistake is.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5QNBEUUFXR4JQ6THYD6ND5YZIHBSHJCZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-03 Thread Chris Angelico
On Mon, 3 Apr 2023 at 17:57, Benedict Verhegghe  wrote:
>
> Well, it's not an object of type 'type' like the list or dict mentioned
> by the OP, for which len() give a
> TypeError: object of type 'type' has no len()

I'm not sure what you mean here. Enum is of type EnumMeta, which is a
subclass of type. That means that, according to the normal rules of
type hierarchy, Enum is indeed a type.

Types add functionality that the parent type (with some Exceptions),
so it should be no surprise that EnumMeta can add a __len__ method
that type itself didn't have.

> Any derived class of Enum will also return True for
> isinstance(..., type).

Yes, that is correct. Any subclass of Enum will also be a type, and
will have a length.

>>> class TestEnum(Enum):
... SPAM = 1
... HAM = 2
...
>>> isinstance(TestEnum, type)
True
>>> len(TestEnum)
2

> Should the derived classes then neither have a
> meaningful result for len()? Enum and derived classes hold a container
> with a fixed set of values. It makes perfectly sense to ask for the
> number of possible values, even when there are none.
>

Yeah, and I'm of the opinion that it's not a problem for Enum to have
zero possible values, but to have the concept of values. Although it
also wouldn't be a problem if it didn't.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7BC4WQ5SN5AJBWOOWOUVWW7JK2RUSNDQ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-03 Thread Benedict Verhegghe
Well, it's not an object of type 'type' like the list or dict mentioned 
by the OP, for which len() give a

TypeError: object of type 'type' has no len()

Any derived class of Enum will also return True for
isinstance(..., type). Should the derived classes then neither have a 
meaningful result for len()? Enum and derived classes hold a container 
with a fixed set of values. It makes perfectly sense to ask for the 
number of possible values, even when there are none.



Op 3/04/2023 om 08:53 schreef Chris Angelico:

On Mon, 3 Apr 2023 at 15:54, Benedict Verhegghe  wrote:


Enum is not a type, like the OP suggested:



Well, it is:


isinstance(Enum, type)

True

but it has a metaclass, meaning that the type of Enum is a subclass of type.


type(Enum)



type(Enum).__bases__

(,)

I mean, we wouldn't be subclassing it if it weren't a type.

The __len__ method is implemented on the metaclass to allow all Enum
subclasses to have lengths (ditto __iter__ to make them iterable), and
as a consequence of that, Enum itself has all the attributes that it
wants to give to its subclasses.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/V2HVHKTK3HG6CV3NBZFISPV6H5PHTTYL/
Code of Conduct: http://python.org/psf/codeofconduct/

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YMGQRHC6FT63JUOSZZEH4DWRSAIWRRIG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-03 Thread Chris Angelico
On Mon, 3 Apr 2023 at 15:54, Benedict Verhegghe  wrote:
>
> Enum is not a type, like the OP suggested:
>

Well, it is:

>>> isinstance(Enum, type)
True

but it has a metaclass, meaning that the type of Enum is a subclass of type.

>>> type(Enum)

>>> type(Enum).__bases__
(,)

I mean, we wouldn't be subclassing it if it weren't a type.

The __len__ method is implemented on the metaclass to allow all Enum
subclasses to have lengths (ditto __iter__ to make them iterable), and
as a consequence of that, Enum itself has all the attributes that it
wants to give to its subclasses.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/V2HVHKTK3HG6CV3NBZFISPV6H5PHTTYL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-02 Thread Benedict Verhegghe

Enum is not a type, like the OP suggested:

from enum import Enum
class Color(Enum):
WHITE = 1
type(Color)

type(Enum)

type(enum.EnumMeta)

Enum.__len__
>

So to me it looks like Enum is just a generic, empty enum. The __len__ 
method is implemented in EnumMeta.



Op 3/04/2023 om 00:18 schreef Greg Ewing:

On 2/04/23 6:36 pm, Benedict Verhegghe wrote:
An Enum is functionally a container with a limited set of constants. 
Why should it not be legitimate to ask for the number of constants in it?


But Enum itself isn't an enum, it's a constructor for enums.
I think len() just happens to work on it as a side effect
of the way enums are implemented.


___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/T7KLPDDRUWIFLLYNTWW6STDZHTTZXBEU/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-02 Thread Chris Angelico
On Mon, 3 Apr 2023 at 08:28, Greg Ewing  wrote:
>
> On 2/04/23 6:36 pm, Benedict Verhegghe wrote:
> > An Enum is functionally a container with a limited set of constants. Why
> > should it not be legitimate to ask for the number of constants in it?
>
> But Enum itself isn't an enum, it's a constructor for enums.
> I think len() just happens to work on it as a side effect
> of the way enums are implemented.
>

Enum itself is iterable, so the question is: how important is it to
block this? Is it actually a problem that the Enum type acts like it
has no elements?

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/3MYSADUPEVP5DJKMRMFF6JKYQAWKAWXK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-02 Thread Greg Ewing

On 2/04/23 6:36 pm, Benedict Verhegghe wrote:
An Enum is functionally a container with a limited set of constants. Why 
should it not be legitimate to ask for the number of constants in it?


But Enum itself isn't an enum, it's a constructor for enums.
I think len() just happens to work on it as a side effect
of the way enums are implemented.

--
Greg

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6YTC6U27TXS2RA26ZQGLM4AZ3SBHUBR3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-02 Thread Benedict Verhegghe
An Enum is functionally a container with a limited set of constants. Why 
should it not be legitimate to ask for the number of constants in it?



Op 1/04/2023 om 16:45 schreef Richard Hajek:

# Feature or enhancement

`len(Enum)` has no useful meaning and should therefore raise the appropriate 
error

# Pitch

I cannot figure out any scenario, in which len(Enum) should not raise. Enum is 
a type and as any other types, such as `len(list)` or `len(dict)`, and should 
not give any meaningful result on len.

Thoughts?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TQB2HRGRJZ64DDWULU6N7RPIPDDJ72GI/
Code of Conduct: http://python.org/psf/codeofconduct/

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/SYT5QW335KRXJMAG5HSIKKIELE7JFXNP/
Code of Conduct: http://python.org/psf/codeofconduct/