Skip to content
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

Start support for mypy --strict #1515

Merged
merged 28 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d2c1894
Have term .toPython() methods pass with mypy-strict-reviewed consumers
ajnelson-nist Dec 16, 2021
d63bc91
Have prepareQuery() pass with mypy-strict-reviewed consumers
ajnelson-nist Dec 17, 2021
3ce1c4c
Add consumer application to exercise methods with multiple run-time t…
ajnelson-nist Dec 17, 2021
2d1d53c
Test Literals and SPARQL .prepareQuery() with mypy-strict-reviewed co…
ajnelson-nist Dec 17, 2021
3f4d1d0
Avoid use of built-in keyword as variable name
ajnelson-nist Dec 17, 2021
15b1710
Add boolean sample to round-trip test
ajnelson-nist Dec 17, 2021
59b1a37
Avoid implicit re-export that raises mypy --strict error
ajnelson-nist Dec 17, 2021
a210552
Test Literals further with mypy-strict-reviewed consumer
ajnelson-nist Dec 17, 2021
41fc12e
Make Graph.bind() pass with mypy-strict-reviewed consumers
ajnelson-nist Dec 17, 2021
b6aa255
Make Namespace() pass with mypy-strict-reviewed consumers
ajnelson-nist Dec 17, 2021
d7803dc
Test Graph.bind(), Namespace() with mypy-strict-reviewed consumer
ajnelson-nist Dec 17, 2021
cbe4745
Make util.guess_format() pass with mypy-strict-reviewed consumers
ajnelson-nist Dec 17, 2021
fe028e1
Add direct test of util.guess_format()
ajnelson-nist Dec 17, 2021
53ca084
Test util.guess_format() with mypy-strict-reviewed consumer
ajnelson-nist Dec 17, 2021
d58ac2d
Fix mypy settings to run select test files in strict mode
aucampia Dec 19, 2021
3576d5f
Merge branch 'master' into start_updates_for_mypy_strict
ajnelson-nist Dec 21, 2021
f080da9
Merge pull request #8 from aucampia/iwana-20211219T1900-fix_mypy
ajnelson-nist Dec 21, 2021
03d0971
Type __new__ instead of adding __init__
aucampia Dec 21, 2021
afbb7ae
Merge pull request #9 from aucampia/iwana-20211219T2004-type_new
ajnelson-nist Dec 21, 2021
2c6821c
Revert addition of Node.__init__
ajnelson-nist Dec 21, 2021
1440514
Reformat per configured 'black'
ajnelson-nist Dec 21, 2021
480bdf6
Use imported symbol
ajnelson-nist Dec 21, 2021
7bf70e4
Rely on pytest instead of "standalone" test-call mode
ajnelson-nist Dec 21, 2021
bc35352
Rename test program and inline documentation on purpose
ajnelson-nist Dec 21, 2021
fb100cf
Fix typo
ajnelson-nist Dec 21, 2021
7c0665f
Align symbol import style
ajnelson-nist Dec 21, 2021
b82a504
Use symbol-import style
ajnelson-nist Dec 21, 2021
0fd66ce
more guess_format tests
nicholascar Dec 22, 2021
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
Prev Previous commit
Next Next commit
Test Literals further with mypy-strict-reviewed consumer
Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
  • Loading branch information
ajnelson-nist committed Dec 17, 2021
commit a210552b817b92ae948414915223d4d59f4c4ee6
2 changes: 1 addition & 1 deletion .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
black --config black.toml --check ./rdflib || true
flake8 --exit-zero rdflib
mypy --show-error-context --show-error-codes rdflib
mypy --show-error-context --show-error-codes --strict test/test_issue1447.py
mypy --show-error-context --show-error-codes --strict test/test_issue1447.py test/test_literal.py
if [ "${{ matrix.os }}" == "windows-latest" ]
then
pytest -ra --cov
Expand Down
57 changes: 29 additions & 28 deletions test/test_literal.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
from decimal import Decimal
import unittest
import datetime
import typing

import rdflib # needed for eval(repr(...)) below
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, _XSD_BOOLEAN
from rdflib import XSD


class TestLiteral(unittest.TestCase):
def setUp(self):
def setUp(self) -> None:
pass

def test_repr_apostrophe(self):
def test_repr_apostrophe(self) -> None:
a = rdflib.Literal("'")
b = eval(repr(a))
self.assertEqual(a, b)

def test_repr_quote(self):
def test_repr_quote(self) -> None:
a = rdflib.Literal('"')
b = eval(repr(a))
self.assertEqual(a, b)

def test_backslash(self):
def test_backslash(self) -> None:
d = r"""
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
Expand All @@ -37,23 +38,23 @@ def test_backslash(self):
b = list(g.objects())[0]
self.assertEqual(a, b)

def test_literal_from_bool(self):
def test_literal_from_bool(self) -> None:
l = rdflib.Literal(True)
self.assertEqual(l.datatype, rdflib.XSD["boolean"])


class TestNew(unittest.TestCase):
def testCantPassLangAndDatatype(self):
def testCantPassLangAndDatatype(self) -> None:
self.assertRaises(
TypeError, Literal, "foo", lang="en", datatype=URIRef("http://example.com/")
)

def testCantPassInvalidLang(self):
def testCantPassInvalidLang(self) -> None:
self.assertRaises(
ValueError, Literal, "foo", lang="999"
)

def testFromOtherLiteral(self):
def testFromOtherLiteral(self) -> None:
l = Literal(1)
l2 = Literal(l)
self.assertTrue(isinstance(l.value, int))
Expand All @@ -64,7 +65,7 @@ def testFromOtherLiteral(self):
l2 = Literal(l, datatype=rdflib.XSD.integer)
self.assertTrue(isinstance(l2.value, int))

def testDatatypeGetsAutoURIRefConversion(self):
def testDatatypeGetsAutoURIRefConversion(self) -> None:
# drewp disapproves of this behavior, but it should be
# represented in the tests
x = Literal("foo", datatype="http://example.com/")
Expand All @@ -75,22 +76,22 @@ def testDatatypeGetsAutoURIRefConversion(self):


class TestRepr(unittest.TestCase):
def testOmitsMissingDatatypeAndLang(self):
def testOmitsMissingDatatypeAndLang(self) -> None:
self.assertEqual(repr(Literal("foo")), "rdflib.term.Literal('foo')")

def testOmitsMissingDatatype(self):
def testOmitsMissingDatatype(self) -> None:
self.assertEqual(
repr(Literal("foo", lang="en")),
"rdflib.term.Literal('foo', lang='en')",
)

def testOmitsMissingLang(self):
def testOmitsMissingLang(self) -> None:
self.assertEqual(
repr(Literal("foo", datatype=URIRef("http://example.com/"))),
"rdflib.term.Literal('foo', datatype=rdflib.term.URIRef('http://example.com/'))",
)

def testSubclassNameAppearsInRepr(self):
def testSubclassNameAppearsInRepr(self) -> None:
class MyLiteral(Literal):
pass

Expand All @@ -99,7 +100,7 @@ class MyLiteral(Literal):


class TestDoubleOutput(unittest.TestCase):
def testNoDanglingPoint(self):
def testNoDanglingPoint(self) -> None:
"""confirms the fix for https://github.com/RDFLib/rdflib/issues/237"""
vv = Literal("0.88", datatype=_XSD_DOUBLE)
out = vv._literal_n3(use_plain=True)
Expand All @@ -109,19 +110,19 @@ def testNoDanglingPoint(self):
class TestParseBoolean(unittest.TestCase):
"""confirms the fix for https://github.com/RDFLib/rdflib/issues/913"""

def testTrueBoolean(self):
def testTrueBoolean(self) -> None:
test_value = Literal("tRue", datatype=_XSD_BOOLEAN)
self.assertTrue(test_value.value)
test_value = Literal("1", datatype=_XSD_BOOLEAN)
self.assertTrue(test_value.value)

def testFalseBoolean(self):
def testFalseBoolean(self) -> None:
test_value = Literal("falsE", datatype=_XSD_BOOLEAN)
self.assertFalse(test_value.value)
test_value = Literal("0", datatype=_XSD_BOOLEAN)
self.assertFalse(test_value.value)

def testNonFalseBoolean(self):
def testNonFalseBoolean(self) -> None:
test_value = Literal("abcd", datatype=_XSD_BOOLEAN)
self.assertRaises(UserWarning)
self.assertFalse(test_value.value)
Expand All @@ -131,12 +132,12 @@ def testNonFalseBoolean(self):


class TestBindings(unittest.TestCase):
def testBinding(self):
def testBinding(self) -> None:
class a:
def __init__(self, v):
def __init__(self, v: str) -> None:
self.v = v[3:-3]

def __str__(self):
def __str__(self) -> str:
return "<<<%s>>>" % self.v

dtA = rdflib.URIRef("urn:dt:a")
Expand All @@ -152,10 +153,10 @@ def __str__(self):
self.assertEqual(la2.value.v, va.v)

class b:
def __init__(self, v):
def __init__(self, v: str) -> None:
self.v = v[3:-3]

def __str__(self):
def __str__(self) -> str:
return "B%s" % self.v

dtB = rdflib.URIRef("urn:dt:b")
Expand All @@ -166,11 +167,11 @@ def __str__(self):
self.assertEqual(lb.value, vb)
self.assertEqual(lb.datatype, dtB)

def testSpecificBinding(self):
def lexify(s):
def testSpecificBinding(self) -> None:
def lexify(s: str) -> str:
return "--%s--" % s

def unlexify(s):
def unlexify(s: str) -> str:
return s[2:-2]

datatype = rdflib.URIRef("urn:dt:mystring")
Expand All @@ -191,7 +192,7 @@ def unlexify(s):


class TestXsdLiterals(unittest.TestCase):
def test_make_literals(self):
def test_make_literals(self) -> None:
"""
Tests literal construction.
"""
Expand Down Expand Up @@ -239,7 +240,7 @@ def test_make_literals(self):
self.check_make_literals(inputs)

@unittest.expectedFailure
def test_make_literals_ki(self):
def test_make_literals_ki(self) -> None:
"""
Known issues with literal construction.
"""
Expand All @@ -257,7 +258,7 @@ def test_make_literals_ki(self):
]
self.check_make_literals(inputs)

def check_make_literals(self, inputs):
def check_make_literals(self, inputs: typing.Sequence[typing.Tuple[str, URIRef, typing.Optional[type]]]) -> None:
for literal_pair in inputs:
(lexical, _type, value_cls) = literal_pair
with self.subTest(f"testing {literal_pair}"):
Expand Down