Skip to content

Commit 4d8e77d

Browse files
authored
implement no type checking flag (#230)
1 parent da6da64 commit 4d8e77d

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

pycardano/backend/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
from dataclasses import dataclass
44
from typing import Dict, List, Union
55

6-
from typeguard import typechecked
7-
86
from pycardano.address import Address
97
from pycardano.network import Network
108
from pycardano.plutus import ExecutionUnits
119
from pycardano.transaction import Transaction, UTxO
10+
from pycardano.types import typechecked
1211

1312
__all__ = [
1413
"GenesisParameters",

pycardano/serialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626

2727
from cbor2 import CBOREncoder, CBORSimpleValue, CBORTag, dumps, loads, undefined
2828
from pprintpp import pformat
29-
from typeguard import check_type, typechecked
3029

3130
from pycardano.exception import (
3231
DeserializeException,
3332
InvalidArgumentException,
3433
SerializeException,
3534
)
35+
from pycardano.types import typechecked, check_type
3636

3737
__all__ = [
3838
"default_encoder",

pycardano/transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from cbor2 import CBORTag
1212
from nacl.encoding import RawEncoder
1313
from nacl.hash import blake2b
14-
from typeguard import typechecked
1514

1615
from pycardano.address import Address
1716
from pycardano.certificate import Certificate
@@ -39,6 +38,7 @@
3938
default_encoder,
4039
list_hook,
4140
)
41+
from pycardano.types import typechecked
4242
from pycardano.witness import TransactionWitnessSet
4343

4444
__all__ = [

pycardano/types.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1+
import os
2+
from functools import partial
13
from typing import Any, Dict
24

5+
import typeguard
6+
37
# https://github.com/python/typing/issues/182#issuecomment-199532520
48
JsonDict = Dict[str, Any]
9+
10+
11+
def typechecked(func=None, *args, **kwargs):
12+
if os.getenv("PYCARDANO_NO_TYPE_CHECK", "False").lower() in ("true", "1"):
13+
if func is None:
14+
return partial(typechecked, *args, **kwargs)
15+
return func
16+
return typeguard.typechecked(func, *args, **kwargs)
17+
18+
19+
def check_type(*args, **kwargs):
20+
if os.getenv("PYCARDANO_NO_TYPE_CHECK", "False").lower() in ("true", "1"):
21+
return None
22+
return typeguard.check_type(*args, **kwargs)

test/pycardano/test_types.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import typeguard
2+
3+
from pycardano.types import typechecked, check_type
4+
5+
6+
def test_types(monkeypatch):
7+
monkeypatch.setenv("PYCARDANO_NO_TYPE_CHECK", "true")
8+
9+
assert typeguard.typechecked != typechecked
10+
assert typeguard.check_type != check_type
11+
12+
@typechecked
13+
def func1():
14+
pass
15+
16+
@typechecked()
17+
def func2():
18+
pass
19+
20+
check_type()

0 commit comments

Comments
 (0)