[issue19462] Add remove_argument() method to argparse.ArgumentParser

2020-03-23 Thread Alexander Hirner


Alexander Hirner  added the comment:

@paul j3
FWIW, popping optionals from a cache that is maintained in addition to 
self._actions, makes special conflict handling unnecessary.

i.e.:
for option_string in a.option_strings:
parser._option_string_actions.pop(option_string)

--
nosy: +cybertreiber

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



[issue39134] can't construct dataclass as ABC (or runtime check as data protocol)

2020-01-18 Thread Alexander Hirner


Alexander Hirner  added the comment:

In that case, what should the getter return? It doesn't know about the 
implementation of x.
Maybe I'm not getting the idea behind adding getters/setters.

--

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



[issue39134] can't construct dataclass as ABC (or runtime check as data protocol)

2020-01-02 Thread Alexander Hirner


Alexander Hirner  added the comment:

This results in E(x=None).

I'd need to look into that to understand why.

--
Added file: https://bugs.python.org/file48817/dc2_repro.py

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



[issue39134] can't construct dataclass as ABC (or runtime check as data protocol)

2020-01-01 Thread Alexander Hirner


Alexander Hirner  added the comment:

Dropping ABCMeta stops at instantiation. This should be in the dataclass code 
that's been generated.

  File "", line 2, in __init__
AttributeError: can't set attribute


Repro:
```
class QuasiABC:
@property
@abstractmethod
def x(self) -> int: ...

@dataclass(frozen=True)
class E(QuasiABC):
x: int

E(10)
```

Interestingly, frozen=False is giving the same error.

--

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



[issue39134] can't construct dataclass as ABC (or runtime check as data protocol)

2019-12-28 Thread Alexander Hirner


Alexander Hirner  added the comment:

We construct a computational graph and need to validate (or search for possible 
new) edges at runtime. The data is specific to computer vision. One example is 
clipping geometry within 2D boundaries. A minimal implementation of this data 
would be:

@dataclass
class FramedGeometry:
width: PositiveInt
height: PositiveInt
geometry: Geometry

However, different properties are manifest in different encodings. Height, 
width can be meta data from an image database or inherent to a decoded image 
(as np.ndarray.shape). The transform will then 
`dataclasses.replace(geometry=...)` its attribute(s).. If width and height are 
not implemented, another transition is needed to produce them whilst only 
looking into the image header, not fully decoding potentially large and many 
images.

The read-only interface ensures that transitions are generic wrt some forms of 
inputs. The replace interface preserves runtime types.

Inputs and outputs are annotated with @dataclass or tuples of them. Those 
dataclasses are a mixin of base dataclasses that declare concrete properties 
like a URI of an image and ABCs that declare accessors like get_width(self) -> 
PositiveInt. We use @pydantic.dataclass to parse, validate and deserialize 
concrete classes to great extent [0]. 

In order to not implement accessors on top of dataclasses, we'd want that 
abstract properties are compatible with dataclasses and issubclass works with 
data protocols (given the necessary constraints).

PS:
Polymorphism for computer-vision data is an interesting problem. Other 
approaches exist, each with their own struggle to model "traits" the right way 
[1]. E.g., scaling would be valid for `FramedGeometry` since no image data is 
included but invalid when images are referenced but cannot be resized, like:

class EncodedSizedAnnotatedFrame:
width: PositiveInt
height: PositiveInt
image_bin: bytes
geometry: Geometry

Thanks!

[0] https://pydantic-docs.helpmanual.io/usage/dataclasses/
[1] https://github.com/pytorch/vision/issues/1406

--

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



[issue39134] can't construct dataclass as ABC (or runtime check as data protocol)

2019-12-27 Thread Alexander Hirner


Alexander Hirner  added the comment:

Pardon my sloppiness. 

1. That should have been PEP 544. The last point referred to the notion of data 
protocols [0].

2. I think solving this issue for dataclasses would ensure better composition 
with modern libraries and other idioms like Protocol and ABC.


[0] 
https://www.python.org/dev/peps/pep-0544/#runtime-checkable-decorator-and-narrowing-types-by-isinstance

--

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



[issue39134] can't construct dataclass as ABC (or runtime check as data protocol)

2019-12-26 Thread Alexander Hirner


Alexander Hirner  added the comment:

Here, nothing but less boiler plate. 

Wouldn't it pay off to not rewrite dataclass features like frozen, replace, 
runtime refelection and the ability to use dataclass aware serialization 
libraries (e.g. pydantic)? I think @dataclass+@abstractproperty behaviour is 
yet to be defined.

The second part about subclass check is not specific to dataclasses.

--

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



[issue39134] can't construct dataclass as ABC (or runtime check as data protocol)

2019-12-25 Thread Alexander Hirner


New submission from Alexander Hirner :

At runtime, we want to check whether objects adhere to a data protocol. This is 
not possible due to problematic interactions between ABC and @dataclass.

The attached file tests all relevant yet impossible cases. Those are:

1) A(object): Can't check due to "Protocols with non-method members don't 
support issubclass()" (as outlined in PEP 554)
2) B(ABC): "Can't instantiate abstract class B with abstract methods x, y"
3) C(Protocol): same as A or same as B if @property is @abstractmethod

The problem can be solved in two parts. First allowing to implement 
@abstractproperty in a dataclass (B). This doesn't involve typing and enables 
the expected use case of dataclass+ABC. I analysed this problem as follows:
Abstract properties evaluate to a default of property, not to 
dataclasses.MISSING. Hence, `dataclasses._init_fn` throws TypeError because of 
deriving from class vars without defaults.

Second, eliding the exception of @runtime_checkable Protocols with non-method 
members if and only if the the the protocol is in its MRO. I didn't think that 
through fully, but instantiation could e.g. fail for missing implementations as 
expected from ABC behaviour (see case D in attached file). I'm not sure about 
the runtime overhead of this suggestion.

--
files: dc_repro.py
messages: 358876
nosy: cybertreiber, eric.smith
priority: normal
severity: normal
status: open
title: can't construct dataclass as ABC (or runtime check as data protocol)
type: behavior
versions: Python 3.6, Python 3.8
Added file: https://bugs.python.org/file48802/dc_repro.py

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