Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions disnake/ext/formatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(
allowed_mapping: Mapping[Type, Set[str]] = MISSING,
*,
suppress_blocked_errors: bool = False,
suppress_nonexistent_key_errors: bool = False,
):
if allowed_mapping is MISSING:
self.allowed_mapping = {
Expand Down Expand Up @@ -112,7 +113,8 @@ def __init__(
self.allowed_mapping = {}
else:
self.allowed_mapping = allowed_mapping
self.suppress_blocked_errors = suppress_blocked_errors
self.suppress_nonexistent_key_errors: bool = suppress_nonexistent_key_errors
self.suppress_blocked_errors: bool = suppress_blocked_errors

def vformat(self, format_string: str, args: Sequence[Any], kwargs: Mapping[str, Any]) -> str:
if args:
Expand Down Expand Up @@ -169,9 +171,17 @@ def get_top_module(obj: Any):
return True

def get_field(self, field_name: str, args: Sequence[Any], kwargs: Mapping[str, Any]) -> Any:
obj, used_key = super().get_field(field_name, args, kwargs)
attr = field_name[len(used_key) :].lstrip(".")
try:
obj, used_key = super().get_field(field_name, args, kwargs)

except (AttributeError, KeyError):
if not self.suppress_nonexistent_key_errors:
raise

# return the unformatted field_name as a placeholder--essentially a silent error
return "{" + field_name + "}", ""
else:
attr = field_name[len(used_key) :].lstrip(".")
try:
if attr and not attr.isspace():
if not self.allowed_mapping:
Expand All @@ -185,7 +195,7 @@ def get_field(self, field_name: str, args: Sequence[Any], kwargs: Mapping[str, A
if not self.suppress_blocked_errors:
raise

# otherwise return the objects so we silently error
# return the unformatted field_name as a placeholder--essentially a silent error
return "{" + field_name + "}", ""

return obj, used_key