Even if a class is inherited from ABC
, it can still be instantiated unless it contains abstract methods.
Having the code below, what is the best way to prevent an Identifier
object from being created: Identifier(['get', 'Name'])
?
from abc import ABC
from typing import List
from dataclasses import dataclass
@dataclass
class Identifier(ABC):
sub_tokens: List[str]
@staticmethod
def from_sub_tokens(sub_tokens):
return SimpleIdentifier(sub_tokens) if len(sub_tokens) == 1 else CompoundIdentifier(sub_tokens)
@dataclass
class SimpleIdentifier(Identifier):
pass
@dataclass
class CompoundIdentifier(Identifier):
pass