Skip to content

refactor: exclude SimpleNamespace from utils #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions docs/escaping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ passed to the ``FluentBundle`` constructor or to ``compile_messages``.

An ``escaper`` is an object that defines the following set of attributes. The
object could be a module, or a simple namespace object you could create using
``types.SimpleNamespace`` (or ``fluent_compiler.utils.SimpleNamespace`` on Python 2), or
an instance of a class with appropriate methods defined. The attributes are:
``types.SimpleNamespace``, or an instance of a class with appropriate
methods defined. The attributes are:

- ``name`` - a simple text value that is used in error messages.

Expand Down
3 changes: 2 additions & 1 deletion src/fluent_compiler/escapers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from types import SimpleNamespace

from . import codegen
from .utils import SimpleNamespace


def identity(value):
Expand Down
21 changes: 0 additions & 21 deletions src/fluent_compiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,6 @@ class Any:
Any = Any()


# On Python 3 we could get away with just using a class, but on Python 2
# functions defined in the class body get wrapped with UnboundMethod, which
# causes problems.

try:
from types import SimpleNamespace
except ImportError:
# Python 2 fallback
class SimpleNamespace:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

def __repr__(self):
keys = sorted(self.__dict__)
items = (f"{k}={self.__dict__[k]!r}" for k in keys)
return f"{type(self).__name__}({', '.join(items)})"

def __eq__(self, other):
return self.__dict__ == other.__dict__


# From spec:
# NamedArgument ::= Identifier blank? ":" blank? (StringLiteral | NumberLiteral)
# Identifier ::= [a-zA-Z] [a-zA-Z0-9_-]*
Expand Down
2 changes: 1 addition & 1 deletion tests/test_compiler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
from types import SimpleNamespace

from markupsafe import Markup, escape

from fluent_compiler import codegen
from fluent_compiler.compiler import compile_messages
from fluent_compiler.errors import FluentCyclicReferenceError, FluentFormatError, FluentReferenceError
from fluent_compiler.resource import FtlResource
from fluent_compiler.utils import SimpleNamespace

from .test_codegen import decompile_ast_list, normalize_python
from .utils import dedent_ftl
Expand Down