How to create an optional field in a dataclass that is inherited? [duplicate]

The underlying problem that you have seems to be the same one that is described here. The short version of that post is that in a function signature (including the dataclass-generated __init__ method), obligatory arguments (like NamedEvent’s name) can not follow after arguments with default values (which are necessary to define the behavior of Event’s … Read more

How can I fix this “ValueError: can’t have unbuffered text I/O” in python 3?

From open‘s docstring: … buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode) … So change inFile = open(WORDLIST_FILENAME, ‘r’, 0) to inFile = open(WORDLIST_FILENAME, ‘r’), or to inFile = open(WORDLIST_FILENAME, ‘rb’, 0) if you really need it (which I doubt).

How to type hint a Callable of a function with default arguments?

Define this: class Foo(Protocol): def __call__(self, x: int = …, /) -> float: … then type hint foo as Foo instead of Callable[[int], float]. Callback protocols allow you to: define flexible callback types that are hard (or even impossible) to express using the Callable[…] syntax and optional arguments are one of those impossible things to … Read more

Why use classmethod instead of staticmethod? [duplicate]

There’s little difference in your example, but suppose you created a subclass of Foo and called the create_new method on the subclass… class Bar(Foo): pass obj = Bar.create_new() …then this base class would cause a new Bar object to be created… class Foo: @classmethod def create_new(cls): return cls() …whereas this base class would cause a … Read more

Getting empty tick labels before showing a plot in Matplotlib

You’ve correctly identified the problem: before you call plt.show() nothing is explicitly set. This is because matplotlib avoids static positioning of the ticks unless it has to, because you’re likely to want to interact with it: if you can ax.set_xlim() for example. In your case, you can draw the figure canvas with fig.canvas.draw() to trigger … Read more

Difference between MappingProxyType and PEP 416 frozendict

TL;DR MappingProxyType is a read only proxy for mapping (e.g. dict) objects. frozendict is an immutable dict Answer The proxy pattern is (quoting wikipedia): A proxy, in its most general form, is a class functioning as an interface to something else. The MappingProxyType is just a simple proxy (i.e. interface) to access the real object … Read more

TypeError: b’1′ is not JSON serializable

This is happening because you’re passing a bytes object in the data dict (b’1′, specifically), probably as the value of index. You need to decode it to a str object before json.dumps can work with it: data = { “body”: email.decode(‘utf-8’), “query_id”: index.decode(‘utf-8’), # decode it here “debug”: 1, “client_id”: “1”, “campaign_id”: 1, “meta”: {“content_type”: … Read more