|
18 | 18 | from functools import cached_property |
19 | 19 | from typing import Any, NamedTuple, TypeVar |
20 | 20 |
|
21 | | -from attr._props import ClassProps |
22 | | - |
23 | 21 | # We need to import _compat itself in addition to the _compat members to avoid |
24 | 22 | # having the thread-local in the globals here. |
25 | 23 | from . import _compat, _config, setters |
@@ -2798,6 +2796,127 @@ def default(self, meth): |
2798 | 2796 | _CountingAttr = _add_eq(_add_repr(_CountingAttr)) |
2799 | 2797 |
|
2800 | 2798 |
|
| 2799 | +class ClassProps: |
| 2800 | + """ |
| 2801 | + Effective class properties as derived from parameters to attr.s() or |
| 2802 | + define() decorators. |
| 2803 | +
|
| 2804 | + .. versionadded:: 25.4.0 |
| 2805 | + """ |
| 2806 | + |
| 2807 | + class Hashability(enum.Enum): |
| 2808 | + """ |
| 2809 | + The hashability of a class. |
| 2810 | +
|
| 2811 | + .. versionadded:: 25.4.0 |
| 2812 | + """ |
| 2813 | + |
| 2814 | + HASHABLE = "hashable" |
| 2815 | + """Write a ``__hash__``.""" |
| 2816 | + HASHABLE_CACHED = "hashable_cache" |
| 2817 | + """Write a ``__hash__`` and cache the hash.""" |
| 2818 | + UNHASHABLE = "unhashable" |
| 2819 | + """Set ``__hash__`` to ``None``.""" |
| 2820 | + LEAVE_ALONE = "leave_alone" |
| 2821 | + """Don't touch ``__hash__``.""" |
| 2822 | + |
| 2823 | + class KeywordOnly(enum.Enum): |
| 2824 | + """ |
| 2825 | + How attributes should be treated regarding keyword-only parameters. |
| 2826 | +
|
| 2827 | + .. versionadded:: 25.4.0 |
| 2828 | + """ |
| 2829 | + |
| 2830 | + NO = "no" |
| 2831 | + """Attributes are not keyword-only.""" |
| 2832 | + YES = "yes" |
| 2833 | + """Attributes in current class without kw_only=False are keyword-only.""" |
| 2834 | + FORCE = "force" |
| 2835 | + """All attributes are keyword-only.""" |
| 2836 | + |
| 2837 | + __slots__ = ( # noqa: RUF023 -- order matters for __init__ |
| 2838 | + "is_exception", |
| 2839 | + "is_slotted", |
| 2840 | + "has_weakref_slot", |
| 2841 | + "is_frozen", |
| 2842 | + "kw_only", |
| 2843 | + "collect_by_mro", |
| 2844 | + "init", |
| 2845 | + "repr", |
| 2846 | + "eq", |
| 2847 | + "order", |
| 2848 | + "hash", |
| 2849 | + "match_args", |
| 2850 | + "str", |
| 2851 | + "getstate_setstate", |
| 2852 | + "on_setattr", |
| 2853 | + "field_transformer", |
| 2854 | + ) |
| 2855 | + |
| 2856 | + def __init__( |
| 2857 | + self, |
| 2858 | + is_exception, |
| 2859 | + is_slotted, |
| 2860 | + has_weakref_slot, |
| 2861 | + is_frozen, |
| 2862 | + kw_only, |
| 2863 | + collect_by_mro, |
| 2864 | + init, |
| 2865 | + repr, |
| 2866 | + eq, |
| 2867 | + order, |
| 2868 | + hash, |
| 2869 | + match_args, |
| 2870 | + str, |
| 2871 | + getstate_setstate, |
| 2872 | + on_setattr, |
| 2873 | + field_transformer, |
| 2874 | + ): |
| 2875 | + self.is_exception = is_exception |
| 2876 | + self.is_slotted = is_slotted |
| 2877 | + self.has_weakref_slot = has_weakref_slot |
| 2878 | + self.is_frozen = is_frozen |
| 2879 | + self.kw_only = kw_only |
| 2880 | + self.collect_by_mro = collect_by_mro |
| 2881 | + self.init = init |
| 2882 | + self.repr = repr |
| 2883 | + self.eq = eq |
| 2884 | + self.order = order |
| 2885 | + self.hash = hash |
| 2886 | + self.match_args = match_args |
| 2887 | + self.str = str |
| 2888 | + self.getstate_setstate = getstate_setstate |
| 2889 | + self.on_setattr = on_setattr |
| 2890 | + self.field_transformer = field_transformer |
| 2891 | + |
| 2892 | + @property |
| 2893 | + def is_hashable(self): |
| 2894 | + return ( |
| 2895 | + self.hash is ClassProps.Hashability.HASHABLE |
| 2896 | + or self.hash is ClassProps.Hashability.HASHABLE_CACHED |
| 2897 | + ) |
| 2898 | + |
| 2899 | + |
| 2900 | +_cas = [ |
| 2901 | + Attribute( |
| 2902 | + name=name, |
| 2903 | + default=NOTHING, |
| 2904 | + validator=None, |
| 2905 | + repr=True, |
| 2906 | + cmp=None, |
| 2907 | + eq=True, |
| 2908 | + order=False, |
| 2909 | + hash=True, |
| 2910 | + init=True, |
| 2911 | + inherited=False, |
| 2912 | + alias=_default_init_alias_for(name), |
| 2913 | + ) |
| 2914 | + for name in ClassProps.__slots__ |
| 2915 | +] |
| 2916 | + |
| 2917 | +ClassProps = _add_eq(_add_repr(ClassProps, attrs=_cas), attrs=_cas) |
| 2918 | + |
| 2919 | + |
2801 | 2920 | class Factory: |
2802 | 2921 | """ |
2803 | 2922 | Stores a factory callable. |
|
0 commit comments