Skip to content

Commit

Permalink
Support creating new INamed instances via classmethod (#886)
Browse files Browse the repository at this point in the history
Additional fix for #884

Co-authored-by: Christopher Rink <christopher@ChristophersMBP.cjlocal>
  • Loading branch information
chrisrink10 and Christopher Rink authored Feb 21, 2024
1 parent cd62689 commit 386e07d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/basilisp/lang/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Union,
)

from typing_extensions import Unpack
from typing_extensions import Self, Unpack

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


T_with_meta = TypeVar("T_with_meta", bound="IWithMeta")


class IWithMeta(IMeta):
__slots__ = ()

@abstractmethod
def with_meta(self: T_with_meta, meta: "Optional[IPersistentMap]") -> T_with_meta:
def with_meta(self, meta: "Optional[IPersistentMap]") -> Self:
raise NotImplementedError()


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

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


ILispObject = _LispObject

Expand Down
4 changes: 4 additions & 0 deletions src/basilisp/lang/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def name(self) -> str:
def ns(self) -> Optional[str]:
return self._ns

@classmethod
def with_name(cls, name: str, ns: Optional[str] = None) -> "Keyword":
return keyword(name, ns=ns)

def _lrepr(self, **kwargs: Unpack[PrintSettings]) -> str:
if self._ns is not None:
return f":{self._ns}/{self._name}"
Expand Down
4 changes: 4 additions & 0 deletions src/basilisp/lang/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def name(self) -> str:
def ns(self) -> Optional[str]:
return self._ns

@classmethod
def with_name(cls, name: str, ns: Optional[str] = None) -> "Symbol":
return Symbol(name, ns=ns)

@property
def meta(self) -> Optional[IPersistentMap]:
return self._meta
Expand Down

0 comments on commit 386e07d

Please sign in to comment.