Skip to content

Commit

Permalink
rename @sentinel_attribute -> @decorated_type_checkable
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Nov 25, 2019
1 parent 4ccd5df commit 6c0c8b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/python/pants/engine/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections.abc import Iterable
from typing import Generic, Iterator, TypeVar

from pants.util.meta import sentinel_attribute
from pants.util.meta import decorated_type_checkable


class SerializationError(Exception):
Expand Down Expand Up @@ -171,7 +171,7 @@ def __bool__(self) -> bool:
return bool(self.dependencies)


@sentinel_attribute
@decorated_type_checkable
def union(cls):
"""A class decorator which other classes can specify that they can resolve to with `UnionRule`.
Expand Down
10 changes: 5 additions & 5 deletions src/python/pants/util/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,23 @@ def staticproperty(func: Union[staticmethod, Callable]) -> ClassPropertyDescript
class _ClassDecoratorWithSentinelAttribute(ABC):
"""Base class to wrap a class decorator which sets a "sentinel attribute".
This functionality is exposed via the `@sentinel_attribute` decorator.
This functionality is exposed via the `@decorated_type_checkable` decorator.
"""

@abstractmethod
def __call__(self, cls: Type) -> Type: ...

def define_instance_of(self, obj: Type, **kwargs) -> Type:
return type(obj.__name__, (obj,), {
'_sentinel_attribute_type': type(self),
'_decorated_type_checkable_type': type(self),
**kwargs
})

def is_instance(self, obj: Type) -> bool:
return getattr(obj, '_sentinel_attribute_type', None) is type(self)
return getattr(obj, '_decorated_type_checkable_type', None) is type(self)


def sentinel_attribute(decorator: Callable[[Type], Type]) -> _ClassDecoratorWithSentinelAttribute:
def decorated_type_checkable(decorator: Callable[[Type], Type]) -> _ClassDecoratorWithSentinelAttribute:
"""Wraps a class decorator to add a "sentinel attribute" to decorated classes.
A "sentinel attribute" is an attribute added to the wrapped class decorator's result with
Expand All @@ -151,7 +151,7 @@ def __call__(self, cls: Type) -> Type:
return WrappedFunction()


@sentinel_attribute
@decorated_type_checkable
def frozen_after_init(cls: Type[_T]) -> Type[_T]:
"""Class decorator to freeze any modifications to the object after __init__() is done.
Expand Down
8 changes: 4 additions & 4 deletions tests/python/pants_test/util/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SingletonMetaclass,
classproperty,
frozen_after_init,
sentinel_attribute,
decorated_type_checkable,
staticproperty,
)

Expand Down Expand Up @@ -254,16 +254,16 @@ def f(cls):

class SentinelAttributeTest(unittest.TestCase):

def test_sentinel_attribute(self):
@sentinel_attribute
def test_decorated_type_checkable(self):
@decorated_type_checkable
def f(cls):
return f.define_instance_of(cls)

@f
class C:
pass

self.assertEqual(C._sentinel_attribute_type, type(f))
self.assertEqual(C._decorated_type_checkable_type, type(f))
self.assertTrue(f.is_instance(C))


Expand Down

0 comments on commit 6c0c8b3

Please sign in to comment.