Skip to content

Commit

Permalink
subscripts work again
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph committed Jan 12, 2024
1 parent 88151eb commit bcd59a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/twisted/logger/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ def __init__(self, wrapped: object) -> None:
def __getattr__(self, name: str) -> object:
return keycall(name, self._wrapped.__getattribute__)

def __getitem__(self, name: str) -> object:
# The sub-object may not be indexable, but if it isn't, that's the
# caller's problem.
value = self._wrapped[name] # type:ignore[index]
return PotentialCallWrapper(value)

def __format__(self, format_spec: str) -> str:
return self._wrapped.__format__(format_spec)

Expand Down
18 changes: 17 additions & 1 deletion src/twisted/logger/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Test cases for L{twisted.logger._format}.
"""

from typing import AnyStr, Optional, cast
from typing import AnyStr, Dict, Optional, cast

try:
from time import tzset
Expand Down Expand Up @@ -95,6 +95,22 @@ def where(self) -> str:
"hello world", self.format("hello {what.where()}", what=World())
)

def test_formatAttributeSubscript(self) -> None:
"""
L{formatEvent} will format subscripts of attributes per PEP 3101.
"""

class Example(object):
config: Dict[str, str] = dict(foo="bar", baz="qux")

self.assertEqual(
"bar qux",
self.format(
"{example.config[foo]} {example.config[baz]}",
example=Example(),
),
)

def test_formatEventNoFormat(self) -> None:
"""
Formatting an event with no format.
Expand Down

0 comments on commit bcd59a8

Please sign in to comment.