-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
The documentation makes brief mention that acp.Abstract is different from a class being abstract in the abc sense, but it might still be surprising to users that the following code works:
import abstractcp as acp
import re
class Parser(acp.Abstract):
PATTERN: str = acp.abstract_class_property(str)
@classmethod
def parse(cls, s):
m = re.fullmatch(cls.PATTERN, s)
if not m:
raise ValueError(s)
return cls(**m.groupdict())
class FooBarParser(Parser):
PATTERN = r"foo\s+bar"
def __init__(self):
super().__init__()
p = Parser() # OK!
print('p is a ', p)
Of course p.parse("a_string") fails in the re.fullmatch function call. In practice it seems like most users will want to define classes which are both acp.Abstract and abstract in the abc-module sense, so it might be useful to show this explicitly in at least one example, maybe:
import abstractcp as acp
from abc import ABC, abstractmethod
import re
class Parser(acp.Abstract, ABC):
PATTERN: str = acp.abstract_class_property(str)
@classmethod
def parse(cls, s):
m = re.fullmatch(cls.PATTERN, s)
if not m:
raise ValueError(s)
return cls(**m.groupdict())
@abstractmethod
def __init__(self): pass
class FooBarParser(Parser):
PATTERN = r"foo\s+bar"
def __init__(self):
"""Make this class concrete."""
super().__init__()
fbp = FooBarParser() # OK
p = Parser() # fails, as intended
Metadata
Metadata
Assignees
Labels
No labels