Skip to content

Commit fc2bc07

Browse files
Merge pull request #6 from brandonwillard/fix-noncons-detection
Check NonCons type in car and cdr dispatch
2 parents cf170b0 + 2f7a17b commit fc2bc07

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ python:
77
- "3.8"
88
- "pypy3"
99

10+
env:
11+
global:
12+
- COVERALLS_PARALLEL=true
13+
1014
install:
1115
- pip install -r requirements.txt
1216

@@ -19,3 +23,6 @@ script:
1923

2024
after_success:
2125
- coveralls
26+
27+
notifications:
28+
webhooks: https://coveralls.io/webhook

cons/core.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ConsNull(metaclass=ConsNullType):
6666

6767
@abstractmethod
6868
def __init__(self):
69-
pass
69+
raise NotImplementedError()
7070

7171

7272
class ConsPair(metaclass=ConsType):
@@ -177,7 +177,7 @@ class MaybeCons(metaclass=MaybeConsType):
177177

178178
@abstractmethod
179179
def __init__(self):
180-
pass
180+
raise NotImplementedError()
181181

182182

183183
class NonCons(ABC):
@@ -189,7 +189,7 @@ class NonCons(ABC):
189189

190190
@abstractmethod
191191
def __init__(self):
192-
pass
192+
raise NotImplementedError()
193193

194194

195195
for t in (type(None), str, set, UserString, ByteString):
@@ -238,6 +238,11 @@ def _car_OrderedDict(z):
238238
return first(z.items())
239239

240240

241+
@_car.register(NonCons)
242+
def _car_NonCons(z):
243+
raise ConsError(f"{z} is a NonCons type")
244+
245+
241246
def cdr(z):
242247
if issubclass(type(z), ConsPair):
243248
return z.cdr
@@ -267,3 +272,8 @@ def _cdr_OrderedDict(z):
267272
if len(z) == 0:
268273
raise ConsError("Not a cons pair")
269274
return cdr(list(z.items()))
275+
276+
277+
@_cdr.register(NonCons)
278+
def _cdr_NonCons(z):
279+
raise ConsError(f"{z} is a NonCons type")

tests/test_cons.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ def test_cons_str():
123123

124124
def test_car_cdr():
125125

126+
with pytest.raises(ConsError):
127+
car(object())
128+
126129
with pytest.raises(ConsError):
127130
car(None)
128131

@@ -135,6 +138,12 @@ def test_car_cdr():
135138
with pytest.raises(ConsError):
136139
car(iter([]))
137140

141+
with pytest.raises(ConsError):
142+
car("ab")
143+
144+
with pytest.raises(ConsError):
145+
cdr(object())
146+
138147
with pytest.raises(ConsError):
139148
cdr(None)
140149

@@ -153,6 +162,9 @@ def test_car_cdr():
153162
with pytest.raises(ConsError):
154163
cdr(OrderedDict())
155164

165+
with pytest.raises(ConsError):
166+
cdr("ab")
167+
156168
assert car([1, 2]) == 1
157169
assert cdr([1, 2]) == [2]
158170

0 commit comments

Comments
 (0)