Skip to content

Documentation: Abstract class can still be instantiated #5

@c84925

Description

@c84925

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions