Skip to content

Commit 386e07d

Browse files
chrisrink10Christopher Rink
andauthored
Support creating new INamed instances via classmethod (#886)
Additional fix for #884 Co-authored-by: Christopher Rink <[email protected]>
1 parent cd62689 commit 386e07d

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/basilisp/lang/interfaces.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Union,
1616
)
1717

18-
from typing_extensions import Unpack
18+
from typing_extensions import Self, Unpack
1919

2020
from basilisp.lang.obj import LispObject as _LispObject
2121
from basilisp.lang.obj import PrintSettings, seq_lrepr
@@ -98,14 +98,11 @@ def meta(self) -> Optional["IPersistentMap"]:
9898
raise NotImplementedError()
9999

100100

101-
T_with_meta = TypeVar("T_with_meta", bound="IWithMeta")
102-
103-
104101
class IWithMeta(IMeta):
105102
__slots__ = ()
106103

107104
@abstractmethod
108-
def with_meta(self: T_with_meta, meta: "Optional[IPersistentMap]") -> T_with_meta:
105+
def with_meta(self, meta: "Optional[IPersistentMap]") -> Self:
109106
raise NotImplementedError()
110107

111108

@@ -122,6 +119,12 @@ def name(self) -> str:
122119
def ns(self) -> Optional[str]:
123120
raise NotImplementedError()
124121

122+
@classmethod
123+
@abstractmethod
124+
def with_name(cls, name: str, ns: Optional[str] = None) -> Self:
125+
"""Create a new instance of this INamed with `name` and optional `ns`."""
126+
raise NotImplementedError()
127+
125128

126129
ILispObject = _LispObject
127130

src/basilisp/lang/keyword.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def name(self) -> str:
3535
def ns(self) -> Optional[str]:
3636
return self._ns
3737

38+
@classmethod
39+
def with_name(cls, name: str, ns: Optional[str] = None) -> "Keyword":
40+
return keyword(name, ns=ns)
41+
3842
def _lrepr(self, **kwargs: Unpack[PrintSettings]) -> str:
3943
if self._ns is not None:
4044
return f":{self._ns}/{self._name}"

src/basilisp/lang/symbol.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def name(self) -> str:
4747
def ns(self) -> Optional[str]:
4848
return self._ns
4949

50+
@classmethod
51+
def with_name(cls, name: str, ns: Optional[str] = None) -> "Symbol":
52+
return Symbol(name, ns=ns)
53+
5054
@property
5155
def meta(self) -> Optional[IPersistentMap]:
5256
return self._meta

0 commit comments

Comments
 (0)