Skip to content

Commit

Permalink
Fix issue when a namespace is named 'self' (#217)
Browse files Browse the repository at this point in the history
Fixes #216
  • Loading branch information
facelessuser committed Mar 19, 2021
1 parent d90d9fd commit 01fb254
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/src/markdown/about/changelog.md
@@ -1,5 +1,9 @@
# Changelog

## 2.2.1

- **FIX**: Fix an issue with namespaces when one of the keys is `self`.

## 2.2

- **NEW**: `:link` and `:any-link` no longer include `#!html <link>` due to a change in the level 4 selector
Expand Down
4 changes: 2 additions & 2 deletions soupsieve/__init__.py
Expand Up @@ -44,11 +44,11 @@ def compile(pattern, namespaces=None, flags=0, **kwargs): # noqa: A001
"""Compile CSS pattern."""

if namespaces is not None:
namespaces = ct.Namespaces(**namespaces)
namespaces = ct.Namespaces(namespaces)

custom = kwargs.get('custom')
if custom is not None:
custom = ct.CustomSelectors(**custom)
custom = ct.CustomSelectors(custom)

if isinstance(pattern, SoupSieve):
if flags:
Expand Down
2 changes: 1 addition & 1 deletion soupsieve/__meta__.py
Expand Up @@ -188,5 +188,5 @@ def parse_version(ver):
return Version(major, minor, micro, release, pre, post, dev)


__version_info__ = Version(2, 2, 0, "final")
__version_info__ = Version(2, 2, 1, "final")
__version__ = __version_info__._get_canonical()
16 changes: 7 additions & 9 deletions soupsieve/css_types.py
Expand Up @@ -89,18 +89,18 @@ def __repr__(self): # pragma: no cover
class ImmutableDict(Mapping):
"""Hashable, immutable dictionary."""

def __init__(self, *args, **kwargs):
def __init__(self, arg):
"""Initialize."""

arg = args[0] if args else kwargs
arg
is_dict = isinstance(arg, dict)
if (
is_dict and not all([isinstance(v, Hashable) for v in arg.values()]) or
not is_dict and not all([isinstance(k, Hashable) and isinstance(v, Hashable) for k, v in arg])
):
raise TypeError('All values must be hashable')

self._d = dict(*args, **kwargs)
self._d = dict(arg)
self._hash = hash(tuple([(type(x), x, type(y), y) for x, y in sorted(self._d.items())]))

def __iter__(self):
Expand Down Expand Up @@ -133,39 +133,37 @@ def __repr__(self): # pragma: no cover
class Namespaces(ImmutableDict):
"""Namespaces."""

def __init__(self, *args, **kwargs):
def __init__(self, arg):
"""Initialize."""

# If there are arguments, check the first index.
# `super` should fail if the user gave multiple arguments,
# so don't bother checking that.
arg = args[0] if args else kwargs
is_dict = isinstance(arg, dict)
if is_dict and not all([isinstance(k, str) and isinstance(v, str) for k, v in arg.items()]):
raise TypeError('Namespace keys and values must be Unicode strings')
elif not is_dict and not all([isinstance(k, str) and isinstance(v, str) for k, v in arg]):
raise TypeError('Namespace keys and values must be Unicode strings')

super(Namespaces, self).__init__(*args, **kwargs)
super(Namespaces, self).__init__(arg)


class CustomSelectors(ImmutableDict):
"""Custom selectors."""

def __init__(self, *args, **kwargs):
def __init__(self, arg):
"""Initialize."""

# If there are arguments, check the first index.
# `super` should fail if the user gave multiple arguments,
# so don't bother checking that.
arg = args[0] if args else kwargs
is_dict = isinstance(arg, dict)
if is_dict and not all([isinstance(k, str) and isinstance(v, str) for k, v in arg.items()]):
raise TypeError('CustomSelectors keys and values must be Unicode strings')
elif not is_dict and not all([isinstance(k, str) and isinstance(v, str) for k, v in arg]):
raise TypeError('CustomSelectors keys and values must be Unicode strings')

super(CustomSelectors, self).__init__(*args, **kwargs)
super(CustomSelectors, self).__init__(arg)


class Selector(Immutable):
Expand Down

0 comments on commit 01fb254

Please sign in to comment.