06:58:10 / 6:58:10 AM
08.04.2025
module tense.types_collection ⬤
≥ 0.3.26b3
Standard ≥ 0.3.?
Available
This declaration can be used anywhere. Declarations written to Tense
Standard List (TSL) are safe from being removed from the project.
This module contains ABCs and utility types, such as after many experiments, classes: Abstract
and FinalVar
. Earlier it was known as module tense.types
and tense.tcs
(to 0.3.26rc3).
Types
Module
tense.types_collection
is full of in-built declarations, including types; some of them refer to non-accessible module
_typeshed
, but also
typing
. Class
FinalVar
is a compromise for not throwing an error when marking a declaration with class
typing.Final
.
* - These declarations are available in previous Python versions, before they were approved to
typing
module. Nevertheless, you need to make sure if you have
typing_extensions
module
installed via PyPI:
pip install typing_extensions
. Feature since 0.3.26rc1.
** - Before Python 3.10, there was a class
ellipsis
, today it has been overtaken by class
EllipsisType
from module
types
.
Qualified name |
Type(s)
|
Version support |
Standard (TSL) |
Status (for version 0.3.27rc2) |
class tense.types_collection.Abstract
≥ 0.3.26b3
Standard ≥ 0.3.26
This class is counterpart of class
abc.ABC
(Abstract Base Class), and creates an abstract class via inheritance. Classes, which extend this class, cannot be initialized.
Before 0.3.27rc1 there was necessity adding the
abstract
keyword into the subclassing section. This class is actually working solution compared to
abc.ABC
, which
actually encourages to use the
metaclass = abc.ABCMeta
construction. This also works in multiple class inheritance.
class AbstractClass(Abstract):
abstract_instanceAbstractClass() tense._exceptions.InitializedError: attempt to initialize an abstract class AbstractClass
You cannot instantiate the
Abstract
class itself, because the class itself is actually a
protocol class.
class tense.types_collection.ClassVar
≥ 0.3.26b3
Standard ≥ 0.3.26
This actually isn't a class, but a generic type alias to factual class
typing.ClassVar
. It converts a variable in a class to a class variable; similar like in case double
decorator invocation:
@classmethod
and
@property
, which isn't possible since version 3.13 of Python.
class Example:
RECONDITE: ClassVar[int]607
eExample()
.print(f{Example.RECONDITE} {e.RECONDITE}) 607 607
Unlike for construction
typing.ClassVar[typing.Final]
, which isn't possible according to
PEP 591, you can actually
do it with class
FinalVar
.
Warning: in this case type will be deduced to
Any
, what binds with this one anomaly: class
FinalVar
loses its value constancy,
what means we can normally do re-assignment:
class Example:
RECONDITE: ClassVar[FinalVar[int]]FinalVar(607)
e = Example()
.print(f{Example.RECONDITE} {e.RECONDITE}) 607 607
Example.RECONDITE5
.print(f{Example.RECONDITE} {e.RECONDITE}) 612 612
A little catchy note: you actually don't need to use the
ClassVar
class, since by default variables inside a class are accessible via both instance and reference:
class Example:
Y67
.print(Example.Y) 67
eExample()
.print(e.Y) 67
That means use of
ClassVar
class has only auxiliary character. But I don't think this class may last in the next Python versions by then.
class tense.types_collection.Final
≥ 0.3.26b3
Standard ≥ 0.3.27
This class is a reference to local class
typing._Final
, and just like that class, before 0.3.27rc1, it required keyword
final
to be used in subclassing section.
Earlier this declaration was a type alias to factual type
typing.Final
, which actually doesn't work as intended - hence class
tense.types_collection.FinalVar
.
This class creates a final class, which disallows its subclasses to be further subclassed. This also works in multiple class inheritance. Example:
from tense.typing_extensions import Final
class FinalClass(Final): OK
class SubclassOfFinalClass(FinalClass): tense._exceptions.SubclassedError: attempt to subclass a final class FinalClass
Before 0.3.27b1 this class was factually called
FinalClass
, and
Final
was actually type alias to
typing.Final
. Projected feature concerned functionality
similar to
enum.Enum
, however, this initiative was handed over to new class
tense.types_collection.Frozen
. Also, if that addition came to force, it
might possibly go to class
tense.types_collection.FinalVar
.
class tense.types_collection.FinalVar
≥ 0.3.26b3
Standard ≥ 0.3.26
Since 0.3.26rc1 you can use it as class-like function from module
tense
-
FinalVar()
. On 0.3.26rc2 this function has been formalized to a class.
This generic class is counterpart of class
typing.Final
, and treats variable as immutable. Syntax:
FinalVar(v)
, where
v
positional parameter is any value.
Before midnight 27th Jul 2024, I did take tests on class
typing.Final
, and even that: variable can be re-assigned anyway. After these experiments I gave up, and instead of
FinalVar
being type alias of
typing.Final
, I formalized it to a class. Below example, with class
typing.Final
I have been also experimenting with:
from typing import Final
f: Final66 OK (in reality it has shown error concerning its syntax)
f3 OK (yes, it suprisingly doesn't throw an error)
fFinalVar(66)
f3 TypeError: Cannot modify a final variable
To receive a value, below are all possible ways:
.print(f) 66
.print(f) 66
.print(f) 66
You cannot do typical operations via
FinalVar
class; you need to retrieve its passed value first from one of methods given above:
f = FinalVar(62)
n = f3 TypeError: unsupported operand type(s) for *: 'FinalVar' and 'int'
n = f3 OK
.print(n) 186
Nothing bad will happen if you did it in this way as well, but I don't think it even is user's intention, since it is quite simple and nothing really changes than printing
a value passed to constructor of the class:
.print(FinalVar(13)) 13; it will behave as Tense.print(13)
I don't prefer this solution, because it also partially wipes out crucial feature of this class - constant value holding.
Class
FinalVar
can be also used in case of variables inside a class:
class Example:
MAXFinalVar(612)
MINFinalVar(36)
e = Example()
_maxe.MAX
_mine.MIN
.print(f{_max} {_min}) 612 36
This will throw an error:
e.MAX8 TypeError: Cannot modify a final variable
Fun with class
FinalVar
doesn't end here: you can also compare 2 instances of this class:
a1FinalVar(())
a2FinalVar(())
.print(a1a2) False
.print(a1a2) True
.print(a1 is a2) False
.print(a1 is not a2) True
.print(a1a2) False
.print(a1a2) True
.print(a1 is a2) False
.print(a1 is not a2) True
i1FinalVar(2024) a lot of work on TensePy
i2FinalVar(2020) year of COVID-19
.print(i1i2) False
.print(i1i2) True
.print(i1 is i2) False
.print(i1 is not i2) True
.print(i1i2) True
.print(i1i2) False
.print(i1i2) True
.print(i1i2) False
.print(i1i2) False
.print(i1i2) True
.print(i1 is i2) False
.print(i1 is not i2) True
.print(i1i2) True
.print(i1i2) False
.print(i1i2) True
.print(i1i2) False
After you retrieve a number value from
FinalVar
class, I could tell you that you can spot something like nonexistent operators
-+
and
+-
, but also - suprisingly - decrementation operator (
--
) and incrementation operator (
++
)! Just look at the following examples:
iFinalVar(32)
fFinalVar(32.6)
.print(i) 32; unary plus: +(+i)
.print(i) -32; unary minus: -(+i)
.print(i) 32; unary plus: +(-i)
.print(i) -32; unary minus: -(-i)
.print(f) 32.6; unary plus: +(+i)
.print(f) -32.6; unary minus: -(+i)
.print(f) 32.6; unary plus: +(-i)
.print(f) -32.6; unary minus: -(-i)
i16
fFinalVar(25)
.print(if) 41; to faciliate it: i + +f (16 + 25)
.print(if) 41; to faciliate it: i + -f (16 + 25)
.print(if) -9; to faciliate it: i - +f (16 - 25)
.print(if) -9; to faciliate it: i - -f (16 - 25)
.print(if) 41; to faciliate it: +i + +f (16 + 25)
.print(if) 41; to faciliate it: +i + -f (16 + 25)
.print(if) -9; to faciliate it: +i - +f (16 - 25)
.print(if) -9; to faciliate it: +i - -f (16 - 25)
.print(if) 9; to faciliate it: -i + +f (-16 + 25)
.print(if) 9; to faciliate it: -i + -f (-16 + 25)
.print(if) -41; to faciliate it: -i - +f (-16 - 25)
.print(if) -41; to faciliate it: -i - -f (-16 - 25)
f1FinalVar(16)
f2FinalVar(25)
.print(f1f2) 41; to faciliate it: +(+f1) + +f2 (16 + 25)
.print(f1f2) 41; to faciliate it: +(+f1) + -f2 (16 + 25)
.print(f1f2) -9; to faciliate it: +(+f1) - +f2 (16 - 25)
.print(f1f2) -9; to faciliate it: +(+f1) - -f2 (16 - 25)
.print(f1f2) 41; to faciliate it: +(-f1) + +f2 (16 + 25)
.print(f1f2) 41; to faciliate it: +(-f1) + -f2 (16 + 25)
.print(f1f2) -9; to faciliate it: +(-f1) - +f2 (16 - 25)
.print(f1f2) -9; to faciliate it: +(-f1) - -f2 (16 - 25)
.print(f1f2) 9; to faciliate it: -(+f1) + +f2 (-16 + 25)
.print(f1f2) 9; to faciliate it: -(+f1) + -f2 (-16 + 25)
.print(f1f2) -41; to faciliate it: -(+f1) - +f2 (-16 - 25)
.print(f1f2) -41; to faciliate it: -(+f1) - -f2 (-16 - 25)
.print(f1f2) 9; to faciliate it: -(-f1) + +f2 (-16 + 25)
.print(f1f2) 9; to faciliate it: -(-f1) + -f2 (-16 + 25)
.print(f1f2) -41; to faciliate it: -(-f1) - +f2 (-16 - 25)
.print(f1f2) -41; to faciliate it: -(-f1) - -f2 (-16 - 25)
Feel free to call all these notations as
double incrementation and
double decrementation. Nevertheless, avoid using any of these examples - I made support for
operators
+
,
-
and
~
in class
FinalVar
just to faciliate and shorten time for conversion to mutable value from immutable
stored in the class. I actually wanted to provide support for pointer only (
*FinalVar
), however, Python finally isn't C - this language just bases on it, skipping
syntax in overall. As much as I want pointer from C to be added (method
__mul__
with one parameter or with different name, like
__ptr__
; referring to
C keyword
nullptr
), I am simultaneously aware that the same operator is used as a tuple parameter. Consider variables, which are instances of
FinalVar
,
as variables, which shouldn't be changed at all (for read-only). You can also use this class as
tense.FinalVar
.
class tense.types_collection.Frozen
≥ 0.3.26b1
Standard ≥ 0.3.27
This class is actually an alias to
dataclasses.dataclass(frozen = True)
. After subclassing this class, this class, after initialization, cannot have its fields modified.
This solution might be also applied to reference, but there is its equivalent during testing -
tense.types_collection.AbstractFrozen
(0.3.27rc1). You
aren't encouraged to use that class yet since it is still being tested. You should use this class if you have version 0.3.27rc1 or newer of Tense.
from tense.typing_extensions import Frozen
class FrozenClass(Frozen):
test62
frozen_class_instanceFrozenClass()
frozen_class_instance.test63 error from module dataclass