Skip to content

Commit eae0777

Browse files
committed
do not type check records of wypp is only imported, fixes #144 and #143
1 parent de96f85 commit eae0777

File tree

7 files changed

+34
-5
lines changed

7 files changed

+34
-5
lines changed

python/fileTests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ checkWithOutputAux yes 1 test-data/testLiteral1.py
288288
checkWithOutputAux yes 0 test-data/testForwardTypeInRecord.py
289289
checkWithOutputAux yes 0 test-data/testForwardTypeInRecord2.py
290290
checkWithOutputAux yes 0 test-data/testUnionOfUnion.py
291+
checkWithOutputAux yes 1 test-data/testRecordTypes.py
291292

292293
function is_min_version()
293294
{

python/src/runner.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,15 @@ def __init__(self, mod, properlyImported):
254254
if name and name[0] != '_':
255255
d[name] = getattr(mod, name)
256256

257-
def prepareLib(onlyCheckRunnable):
257+
def prepareLib(onlyCheckRunnable, enableTypeChecking):
258258
libDefs = None
259259
mod = INSTALLED_MODULE_NAME
260260
verbose('Attempting to import ' + mod)
261261
wypp = importlib.import_module(mod)
262262
libDefs = Lib(wypp, True)
263263
verbose('Successfully imported module ' + mod + ' from file ' + wypp.__file__)
264264
libDefs.initModule(enableChecks=not onlyCheckRunnable,
265+
enableTypeChecking=enableTypeChecking,
265266
quiet=onlyCheckRunnable)
266267
return libDefs
267268

@@ -534,7 +535,7 @@ def main(globals, argList=None):
534535
if not args.checkRunnable and (not args.quiet or args.verbose):
535536
printWelcomeString(fileToRun, version, useUntypy=args.checkTypes)
536537

537-
libDefs = prepareLib(onlyCheckRunnable=args.checkRunnable)
538+
libDefs = prepareLib(onlyCheckRunnable=args.checkRunnable, enableTypeChecking=args.checkTypes)
538539

539540
globals['__name__'] = '__wypp__'
540541
sys.modules['__wypp__'] = sys.modules['__main__']

python/src/writeYourProgram.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ def _setattr(obj, k, v):
141141
def record(cls=None, mutable=False):
142142
def wrap(cls):
143143
newCls = dataclasses.dataclass(cls, frozen=not mutable)
144-
return _patchDataClass(newCls, mutable)
144+
if _typeCheckingEnabled:
145+
return _patchDataClass(newCls, mutable)
146+
else:
147+
return newCls
145148
# See if we're being called as @record or @record().
146149
if cls is None:
147150
# We're called with parens.
@@ -155,17 +158,20 @@ def wrap(cls):
155158
_die = False
156159

157160
def setDieOnCheckFailures(b):
161+
global _die
158162
_die = b
159163

160164
def _dieOnCheckFailures():
161165
return _die
162166

163167
_testCount = {'total': 0, 'failing': 0}
164168
_checksEnabled = True
169+
_typeCheckingEnabled = False
165170

166-
def initModule(enableChecks=True, quiet=False):
167-
global _checksEnabled
171+
def initModule(enableChecks=True, enableTypeChecking=True, quiet=False):
172+
global _checksEnabled, _typeCheckingEnabled
168173
_checksEnabled = enableChecks
174+
_typeCheckingEnabled = enableTypeChecking
169175
resetTestCount()
170176

171177
def resetTestCount():
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Traceback (most recent call last):
2+
File "test-data/testRecordTypes.py", line 8, in <module>
3+
p = Point(1, '5')
4+
WyppTypeError: got value of wrong type
5+
given: '5'
6+
expected: value of type int
7+
8+
context: record constructor Point(x: int, y: int) -> Self
9+
^^^
10+
declared at: test-data/testRecordTypes.py:3
11+
caused by: test-data/testRecordTypes.py:8
12+
| p = Point(1, '5')

python/test-data/testRecordTypes.out

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from wypp import *
2+
3+
@record
4+
class Point:
5+
x: int
6+
y: int
7+
8+
p = Point(1, '5')

python/tests/testRecord.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import traceback
55
import dataclasses
66

7+
initModule()
78
setDieOnCheckFailures(True)
89

910
@record

0 commit comments

Comments
 (0)