[issue44905] Abstract instance and class attributes for abstract base classes

2021-08-16 Thread Jacob Nilsson


Jacob Nilsson  added the comment:

Could one possible downside of this suggestion be, if implemented like in 
https://newbedev.com/python-abstract-class-shall-force-derived-classes-to-initialize-variable-in-init,
 a slowdown in code creating a lot of instances of a class with metaclass 
ABCMeta?

I have personally never experienced that the current behavior is an issue, I've 
only occasionally got confused by the error messages until I read the traceback.

--

___
Python tracker 

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



[issue44905] Abstract instance and class attributes for abstract base classes

2021-08-16 Thread Alex Waygood


Alex Waygood  added the comment:

^And, that only works for class attributes, not instance attributes.

--

___
Python tracker 

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



[issue44905] Abstract instance and class attributes for abstract base classes

2021-08-16 Thread Alex Waygood


Alex Waygood  added the comment:

Tomasz -- as discussed in the original BPO issue for `abstractmethod` 
implementations (https://bugs.python.org/issue1706989), this works as a 
workaround:

```
>>> from abc import ABCMeta
>>> class AbstractAttribute:
... __isabstractmethod__ = True
... 
>>> class Foo(metaclass=ABCMeta):
... abstract_attribute_to_be_overriden = AbstractAttribute()
... 
>>> class Bar(Foo): pass
... 
>>> b = Bar()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Can't instantiate abstract class Bar with abstract method 
abstract_attribute_to_be_overriden
```

It would be nice to have something like this in the `abc` module, though.

--

___
Python tracker 

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



[issue44905] Abstract instance and class attributes for abstract base classes

2021-08-13 Thread Alex Waygood


Alex Waygood  added the comment:

+1 to this suggestion. I had a use case for this the other day. It currently 
feels like something of a discrepancy between ABCs and `typing.Property`, which 
can be seen as analogous in some ways to "an ABC for the static type-checker". 
`typing.Property` allows you to specify that an object must have certain 
attributes, as well as certain methods, while for ABCs it is only the latter.

--
nosy: +AlexWaygood

___
Python tracker 

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



[issue44905] Abstract instance and class attributes for abstract base classes

2021-08-13 Thread Jacob Nilsson


Change by Jacob Nilsson :


--
nosy: +ajoino

___
Python tracker 

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



[issue44905] Abstract instance and class attributes for abstract base classes

2021-08-12 Thread Tomasz Rzepecki


New submission from Tomasz Rzepecki :

There seems to be no way to transparently make an abstract base class enforce 
instance attributes for subclasses (without creating a custom metaclass, see 
e.g. 
https://newbedev.com/python-abstract-class-shall-force-derived-classes-to-initialize-variable-in-init).

The analogous problem for enforcing *class* attributes in subclasses can be 
solved by creating an abstract class property (which can then be overridden by 
a class attribute), but this feels like a hack and possibly a bug (see 
https://bugs.python.org/issue44904 for a related bug).

The corresponding "solution" for instance attributes does not work (see 
attached file), and probably rightly so.

This seems like an oversight to me.

--
files: example.py
messages: 399486
nosy: rzepecki.t
priority: normal
severity: normal
status: open
title: Abstract instance and class attributes for abstract base classes
type: enhancement
versions: Python 3.9
Added file: https://bugs.python.org/file50213/example.py

___
Python tracker 

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