Skip to content

Commit 443c2f2

Browse files
committed
Make namespaces a generator by default
90f6fe5 changed ```python class Store: def namespaces(self): """ """ if False: yield None ``` to ```python class Store: def namespaces(self): """ """ ``` This resulted in `Store.namespaces` no longer being an emtpy generator by default (see https://stackoverflow.com/q/13243766). The reason it was removed is because it is unreachable code, however I did not consider that it would make the function an empty generator. The reason why `if False: yield None` made it an empty generator is because the presence of the yield keyword informs CPython that a function is a generator: https://www.python.org/dev/peps/pep-0255/#why-a-new-keyword-for-yield-why-not-a-builtin-function-instead There are several ways of making an empty generator: - `if False: yield` - `return` and `yield` - `yield from ()` - ... more Both `if False: yield` and `return`; `yield` results in same bytecode however, which is that of an empty function, and they are both equally confusing I would say. `yield from ()` is somewhat clearer, but the bytecode of the funciton looks very different from an empty function (see https://stackoverflow.com/a/61496399) So the best option I see is to just put back what was there and add a comment and a test to prevent the issue from re-occuring.
1 parent 4a38e2e commit 443c2f2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

rdflib/store.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ def namespace(self, prefix):
364364

365365
def namespaces(self):
366366
""" """
367+
# This is here so that the function becomes an empty generator
368+
yield from ()
367369

368370
# Optional Transactional methods
369371

test/test_store.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from rdflib import Graph, URIRef
3+
from tempfile import NamedTemporaryFile, TemporaryDirectory
4+
from pathlib import Path, PurePath
5+
from rdflib.store import Store
6+
7+
8+
class TestBareStore(unittest.TestCase):
9+
def test_namespaces(self):
10+
class MyStore(Store):
11+
pass
12+
13+
my_store = MyStore()
14+
self.assertEqual(list(my_store.namespaces()), [])
15+
16+
17+
if __name__ == "__main__":
18+
unittest.main()

0 commit comments

Comments
 (0)