-
Notifications
You must be signed in to change notification settings - Fork 576
Description
I have a variable-spelling pattern for rdflib.Namespace
objects, prefixing them with NS_
. To keep things consistent between namespaces custom to the script I'm working on, as well as with built-in rdflib
namespaces, I tend to have this in my scripts' module-level variables area:
from rdflib import RDFS
NS_RDFS = RDFS
This hits a weird-looking error from mypy
if I need to use a dictionary spelling for accessing a namespace member. While not common, SHACL happens to include a few namespace members whose suffixes are Python keywords, sh:class
and sh:not
among them.
Here is a shell transcript of a set of Python programs, with comments indicating what fails, and the mypy
report across all of them:
$ mypy --version
mypy 1.3.0 (compiled: yes)
$ head -n 20 try_*
==> try_1.py <==
import rdflib
# This works:
rdflib.DCTERMS["title"]
# And this works:
rdflib.SH["not"]
==> try_2.py <==
from rdflib import SH
# This works:
SH["not"]
==> try_3.py <==
import rdflib
# This works:
rdflib.Namespace(str(rdflib.SH))["not"]
==> try_4.py <==
import rdflib
NS_DCTERMS = rdflib.DCTERMS
NS_SH = rdflib.SH
# This works:
NS_DCTERMS.title
# But this fails:
NS_DCTERMS["title"]
# This works:
NS_SH.alternativePath
# But this fails:
NS_SH["alternativePath"]
# And this fails:
NS_SH["not"]
$ mypy try_*
try_4.py:7: error: Type application targets a non-generic function or class [misc]
try_4.py:7: error: Name "title" is not defined [name-defined]
try_4.py:11: error: Type application targets a non-generic function or class [misc]
try_4.py:11: error: Name "alternativePath" is not defined [name-defined]
try_4.py:13: error: Type application targets a non-generic function or class [misc]
try_4.py:13: error: Invalid type comment or annotation [valid-type]
Found 6 errors in 1 file (checked 4 source files)
It's only try_4.py
that has errors. try_3.py
demonstrates a workaround by stripping out the DefinedNamespace
functionality. For my purposes, that prevents me typo'ing the namespace IRI, but now I lose typo-checking of the namespace members.
Is this an RDFLib issue, or a Mypy issue?