How about an alternative frozen dataclass with a explicit replace,
configure and create methods?

@dataclasses.dataclass(frozen=True)
class SSLContextFactory:
    minimum_version: TLSVersion = TLSVersion.TLSv1_2
    options: ...

    replace = dataclasses.replace

    def configure(self, ctx: SSLContext):
        ctx.mimumum_version = self.minimum_version
        ctx.options = self.options
        ...

    def create(self):
        ctx = SSLContext()
        self.configure(ctx)
        return ctx

On Fri, 25 Jun 2021, 20:52 Guido van Rossum, <gu...@python.org> wrote:

> On Fri, Jun 25, 2021 at 12:17 PM Christian Heimes <christ...@python.org>
> wrote:
>
>> On 25/06/2021 20.17, Guido van Rossum wrote:
>> > On Fri, Jun 25, 2021 at 8:22 AM Bluenix <bluenix...@gmail.com
>> > <mailto:bluenix...@gmail.com>> wrote:
>> >
>> >     I am not fully aware of how ssl.SSLContext is used, but adding
>> >     __slots__ would prevent this. You would see an error similar to:
>> >     AttributeError: 'MyClass' object has no attribute 'my_attribute'
>> >
>> >
>> > That's a reasonable solution, except that it's not backwards compatible.
>> > It's possible that there is code out there that for some reason adds
>> > private attributes to an SSLContext instance, and using __slots__ would
>> > break such usage. (They could perhaps fix their code by using a dummy
>> > subclass, but that could well become a non-trivial change to their code,
>> > depending on where they get their SSLContext instances.)
>> >
>> > So unless there's evidence that nobody does that, we're stuck with the
>> > status quo. I'm adding Christian Heimes to the thread in case he has a
>> > hunch either way.
>>
>> I agree, it is a backwards incompatible change. Also __slots__ won't
>> work. The class has class attributes that can be modified in instances.
>>
>
> Oh, I see. There are two class attributes, sslsocket_class and
> sslobject_class, and their docs say they can be overridden per instance.
> Could we perhaps create a custom descriptor that allows both per-instance
> and per-class assignment and lookup?
>
>
>> You cannot have attributes that are both class and instance attributes
>> with __slots__. We'd have to overwrite __setattr__() and block unknown
>> attributes of exact instances of ssl.SSLContext.
>>
>
> Well, if we don't think it's supported behavior to subclass SSLContext,
> perhaps we could do that. The bug in the OP's code (misspelling
> minimum_version in assignment) is pretty hard to find.
>
> --
> --Guido van Rossum (python.org/~guido)
> *Pronouns: he/him **(why is my pronoun here?)*
> <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
> _______________________________________________
> 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/VGSFW6UXY7NRHZ6OUWJMXABFVVFMHIQN/
> 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/OFABWJUMIUFXQAYFULBNDMIAYGVWOWLH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to