Non-dataclass alternatives to this code #968
Closed
dmos62
announced in
Maintainer discussions
Replies: 1 comment 1 reply
-
I'm not sure all of these are requirements, I don't think we need all the static-ness and immutability. Here's how I would do it: class Expression:
id = None
name = None
def to_sa_expression():
raise NotImplementedError('Expression subclasses should implement a to_sa_expression method')
def suggestions():
return ()
def __init__(self, parameters):
if self.id is None:
raise ValueError('Expression subclasses should define an ID')
if self.name is None:
raise ValueError('Expression subclasses should define a name')
self.parameters = parameters
class ColumnReference(Expression):
id = 'column_reference'
name = 'Column Reference'
def to_sa_expression():
return column(p)
def suggestions():
return (suggestions.parameter_count(1), suggestions.parameter(1, suggestions.column)) Note that I haven't run this code locally, since not all of it seems to be runnable (e.g. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Previously discussed in: #913
Related to: #921
Here's an implementation of an abstract class
Expression
and a concrete subclassColumnReference
, both written usingdataclasses
:Some of the requirements for the code above are:
id
,name
,suggestions
);to_sa_expression
);parameters
);Here's one way to do this without
dataclasses
:@staticmethod
is used for static properties, because there's no such thing as a static property per se in Python. The instance property is implemented using the "hidden" instance-level_parameters
and the abstract-class-levelparameters
property that accesses it: that provides an immutable instance property and its abstract definition.I'm not crazy about the non-dataclass variant above. Anyone care to try their hand at this?
I explored subclassing a
namedtuple
for this, but it didn't seem well suited for the task, though there might be a way.Beta Was this translation helpful? Give feedback.
All reactions