Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
final_class.py
Go to the documentation of this file.
1r"""
2# +==== BEGIN CatFeeder =================+
3# LOGO:
4# ..............(..../\
5# ...............)..(.')
6# ..............(../..)
7# ...............\‍(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
10# animals/cats
11# /STOP
12# PROJECT: CatFeeder
13# FILE: final_class.py
14# CREATION DATE: 22-11-2025
15# LAST Modified: 14:42:50 19-12-2025
16# DESCRIPTION:
17# This is the backend server in charge of making the actual website work.
18# /STOP
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE: The class in charge of reproducing the method `final` from cpp but for python.
21# // AR
22# +==== END CatFeeder =================+
23"""
24
25
26class FinalClass(type):
27 """Final (non-subclassable) metaclass.
28
29 Summary:
30 When a class is declared with ``metaclass=FinalClass`` it becomes *final*:
31 any attempt to subclass it will immediately raise ``TypeError`` at class
32 creation time. This mimics the C++ ``final`` keyword semantics.
33
34 How it works:
35 During metaclass ``__init__`` we inspect each direct base. If one of the
36 bases itself was created with ``FinalClass`` we reject the new class.
37
38 Usage Example:
39 >>> class Service(metaclass=FinalClass):
40 ... pass
41 >>> # This will fail:
42 ... class Derived(Service): # doctest: +IGNORE_EXCEPTION_DETAIL
43 ... pass
44 Traceback (most recent call last):
45 TypeError: Class 'Service' is final and cannot be subclassed.
46
47 Rationale:
48 Preventing inheritance can guard invariants (e.g. security-sensitive
49 logic, lifecycle guarantees) and simplify reasoning about extension
50 points. Prefer composition or explicitly documented interfaces instead.
51
52 Notes:
53 - Only direct subclassing is blocked; mixins inheriting transitively
54 still trigger the same check because the original base appears in
55 ``bases``.
56 - Does not prevent monkey-patching of attributes; Python cannot fully
57 emulate C++ immutability semantics.
58 """
59
60 def __init__(cls, name, bases, namespace):
61 for base in bases:
62 if isinstance(base, FinalClass):
63 raise TypeError(
64 f"Class '{base.__name__}' is final and cannot be subclassed."
65 )
66 super().__init__(name, bases, namespace)
__init__(cls, name, bases, namespace)