Registry / type-stubs / constantly

constantly

JSON →
library23.10.4pypypi✓ verified 24d ago

constantly is a Python library providing symbolic constants, particularly useful for creating enumerated types and immutable values that are easily comparable by identity. It is maintained by the Twisted project, with releases appearing periodically. The current version is 23.10.4.

pip install constantly
INSTALL
IMPORT
SIG · CONSTANTLY
C
constantly
type-stubspythonv23.10.4
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v23.10.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

FlagConstant
from constantly import FlagConstant
from constantly import Constant
Flags
from constantly import Flags
from constantly import Constant
NamedConstant
from constantly import NamedConstant
from constantly import Constant

This example demonstrates how to define a set of named constants using `Values` and `NamedConstant`, and how to access and compare them. `NamedConstant` instances within a `Values` class are singletons.

from constantly import NamedConstant, Values class Fruit(Values): apple = NamedConstant() banana = NamedConstant() orange = NamedConstant() # Accessing constants print(f"Apple constant: {Fruit.apple}") print(f"Apple name: {Fruit.apple.name}") print(f"Apple value: {Fruit.apple.value}") # Comparing constants by identity (preferred) print(f"Is Fruit.apple the same object as Fruit.apple? {Fruit.apple is Fruit.apple}") print(f"Is Fruit.apple the same object as Fruit.banana? {Fruit.apple is Fruit.banana}") # Iterating over constants in the class print("All fruits:") for fruit in Fruit.iterconstants(): print(f"- {fruit.name}")
Debug
Known issues
breakingPython 3.7 support was dropped in `constantly` version 23.10.0.
fix
Ensure your environment uses Python 3.8 or newer. If you need Python 3.7 support, pin `constantly<23.10.0`.
affects: >=23.10.0
gotchaNamedConstants defined within a `Values` class are singletons. Prefer identity comparison (`is`) over value comparison (`==`) for correctness, especially when comparing against other objects or similar strings.
fix
Always use `constant_a is constant_b` for comparing two `NamedConstant` instances. While `==` works for comparing a `NamedConstant` to its string name, `is` ensures you're comparing the exact symbolic object.
affects: all
gotchaAttempting to modify the `name` or `value` attributes of a `Constant` or `NamedConstant` after definition will result in an `AttributeError`, as they are designed to be immutable.
fix
Constants are immutable by design. Define them once and treat them as fixed values throughout your application.
affects: all
breakingAccessing a `NamedConstant` or `Constant` instance that was not properly registered with its `Values` container can lead to an `AttributeError: 'NoneType' object has no attribute '__name__'` when its `__repr__` method is called. This typically happens if the constant's `_container` attribute is `None`.
fix
Ensure all `NamedConstant` and `Constant` instances are defined *within* a `Values` class or properly assigned to their `_container` if created dynamically. Misconfiguration or incorrect usage of the library's `Values` class can result in unassigned containers. For example, if defining constants dynamically, explicitly set the `_container` attribute to the `Values` instance.
affects: all
breakingAttempting to represent (e.g., `print` or `repr()`) a `NamedConstant` instance whose internal `_container` attribute is `None` will result in an `AttributeError: 'NoneType' object has no attribute '__name__'`.
fix
Ensure all `NamedConstant` instances are defined as class attributes within a `constantly.Values` subclass. This allows the `ValuesMetaClass` to correctly set the `_container` attribute for each constant. If you instantiate `NamedConstant` directly, its default `__repr__` will fail unless `_container` is manually set or a custom `__repr__` is provided.
affects: all
Upgrade
Version history
23.10.4latest on PyPI · released Oct 28, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
34 hits · last 30 days
node
30
OpenAI (training)
1
Resources
constantly — pip install constantly · libregistry