From 0a71d956552badf7a1ce6bf2d70497cf997f71ef Mon Sep 17 00:00:00 2001 From: sg495 Date: Tue, 16 Jan 2024 17:29:40 +0000 Subject: [PATCH] Changed backing attributes to private. --- docs/conf.py | 4 ++-- test/test_00_base.py | 2 +- typed_descriptors/__init__.py | 18 ++---------------- typed_descriptors/attr.py | 16 +--------------- typed_descriptors/base.py | 25 ++++++------------------- typed_descriptors/prop.py | 16 +--------------- 6 files changed, 13 insertions(+), 68 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 7d947ac..26b18f1 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -33,9 +33,9 @@ # built documents. # # The full version, including alpha/beta/rc tags. -release = "1.0.0" +release = "1.1.0" # The short X.Y version. -version = "1.0.0" +version = "1.1.0" # -- General configuration --------------------------------------------------- diff --git a/test/test_00_base.py b/test/test_00_base.py index f946bb8..d7957bf 100644 --- a/test/test_00_base.py +++ b/test/test_00_base.py @@ -18,7 +18,7 @@ def test_base_set_name_slots(attr_name: Optional[str]) -> None: if attr_name is not None: slot_name = attr_name else: - slot_name = "_x" + slot_name = "__x" class C: x = Attr(int, lambda _, x: x >= 0, attr_name=attr_name) __slots__ = (slot_name,) diff --git a/typed_descriptors/__init__.py b/typed_descriptors/__init__.py index 237afe8..df9b4f0 100644 --- a/typed_descriptors/__init__.py +++ b/typed_descriptors/__init__.py @@ -2,27 +2,13 @@ Descriptor classes with static and runtime validation features. """ +# typed-descriptors: Descriptor classes for typed attributes and properties. # Copyright (C) 2023 Hashberg Ltd -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. - -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. - -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 -# USA - from __future__ import annotations from .attr import Attr from .prop import Prop -__version__ = "1.0.0" +__version__ = "1.1.0" __all__ = ("Attr", "Prop") diff --git a/typed_descriptors/attr.py b/typed_descriptors/attr.py index d337084..790369f 100644 --- a/typed_descriptors/attr.py +++ b/typed_descriptors/attr.py @@ -2,23 +2,9 @@ Descriptor class for attributes. """ +# Part of typed-descriptors # Copyright (C) 2023 Hashberg Ltd -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. - -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. - -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 -# USA - from __future__ import annotations import sys from typing import ( diff --git a/typed_descriptors/base.py b/typed_descriptors/base.py index c847eb8..f41bc18 100644 --- a/typed_descriptors/base.py +++ b/typed_descriptors/base.py @@ -2,23 +2,9 @@ Abstract base class for descriptors backed by protected/private attributes. """ +# Part of typed-descriptors # Copyright (C) 2023 Hashberg Ltd -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. - -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. - -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 -# USA - from __future__ import annotations from abc import abstractmethod import sys @@ -26,7 +12,6 @@ Any, Generic, Literal, - MutableSequence, Optional, Type, TypeVar, @@ -52,8 +37,9 @@ class DescriptorBase(Generic[T]): - if the optional kwarg ``attr_name`` is specified in the constructor, the descriptor uses an attribute with that given name - - otherwise, the descriptor uses an attribute obtained from the - descriptor name by prepending an underscore + - otherwise, the descriptor uses a private attribute obtained from the + descriptor name by prepending one or two underscores (depending on + whether the descriptor name itself starts with underscore or not, resp.). If the attribute name starts with two underscores but does not end with two underscores, name-mangling is automatically performed. @@ -186,7 +172,8 @@ def __set_name__(self, owner: Type[Any], name: str) -> None: # 2. Get the backing attribute name (not yet name-mangled): given_attr_name = self.__given_attr_name if given_attr_name is None: - given_attr_name = f"_{name}" + attr_name_prefix = "_" if name.startswith("_") else "__" + given_attr_name = f"{attr_name_prefix}{name}" elif given_attr_name == name: raise ValueError( f"Name of backing attribute for descriptor {self.name!r} " diff --git a/typed_descriptors/prop.py b/typed_descriptors/prop.py index a74ed42..40c61d8 100644 --- a/typed_descriptors/prop.py +++ b/typed_descriptors/prop.py @@ -2,23 +2,9 @@ Descriptor class for cached properties. """ +# Part of typed-descriptors # Copyright (C) 2023 Hashberg Ltd -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. - -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. - -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 -# USA - from __future__ import annotations import sys from typing import (