Skip to content

Commit

Permalink
Merge pull request #1626 from edmondchuc/feat/DefinedNamespaces-dir
Browse files Browse the repository at this point in the history
Add __dir__ to DefinedNamespaceMeta.
  • Loading branch information
nicholascar authored Dec 28, 2021
2 parents 292a6bc + 4dce6ff commit 8aa0aa0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import warnings
from typing import TYPE_CHECKING, List, Union
from typing import TYPE_CHECKING, List, Union, Iterable
from unicodedata import category

from pathlib import Path
Expand Down Expand Up @@ -228,6 +228,10 @@ def __contains__(cls, item):
if issubclass(c, DefinedNamespace)
)

def __dir__(cls) -> Iterable[str]:
values = {cls[str(x)] for x in cls.__annotations__}
return values


class DefinedNamespace(metaclass=DefinedNamespaceMeta):
"""
Expand Down
35 changes: 35 additions & 0 deletions test/test_definednamespace_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from rdflib import RDF


def test_definednamespace_dir():
x = dir(RDF)

values = [
RDF.nil,
RDF.direction,
RDF.first,
RDF.language,
RDF.object,
RDF.predicate,
RDF.rest,
RDF.subject,
RDF.type,
RDF.value,
RDF.Alt,
RDF.Bag,
RDF.CompoundLiteral,
RDF.List,
RDF.Property,
RDF.Seq,
RDF.Statement,
RDF.HTML,
RDF.JSON,
RDF.PlainLiteral,
RDF.XMLLiteral,
RDF.langString,
]

assert len(values) == len(x)

for value in values:
assert value in x

0 comments on commit 8aa0aa0

Please sign in to comment.