Skip to content

implement no type checking flag #230

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

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions pycardano/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
from dataclasses import dataclass
from typing import Dict, List, Union

from typeguard import typechecked

from pycardano.address import Address
from pycardano.network import Network
from pycardano.plutus import ExecutionUnits
from pycardano.transaction import Transaction, UTxO
from pycardano.types import typechecked

__all__ = [
"GenesisParameters",
Expand Down
2 changes: 1 addition & 1 deletion pycardano/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

from cbor2 import CBOREncoder, CBORSimpleValue, CBORTag, dumps, loads, undefined
from pprintpp import pformat
from typeguard import check_type, typechecked

from pycardano.exception import (
DeserializeException,
InvalidArgumentException,
SerializeException,
)
from pycardano.types import typechecked, check_type

__all__ = [
"default_encoder",
Expand Down
2 changes: 1 addition & 1 deletion pycardano/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from cbor2 import CBORTag
from nacl.encoding import RawEncoder
from nacl.hash import blake2b
from typeguard import typechecked

from pycardano.address import Address
from pycardano.certificate import Certificate
Expand Down Expand Up @@ -39,6 +38,7 @@
default_encoder,
list_hook,
)
from pycardano.types import typechecked
from pycardano.witness import TransactionWitnessSet

__all__ = [
Expand Down
18 changes: 18 additions & 0 deletions pycardano/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import os
from functools import partial
from typing import Any, Dict

import typeguard

# https://github.com/python/typing/issues/182#issuecomment-199532520
JsonDict = Dict[str, Any]


def typechecked(func=None, *args, **kwargs):
if os.getenv("PYCARDANO_NO_TYPE_CHECK", "False").lower() in ("true", "1"):
if func is None:
return partial(typechecked, *args, **kwargs)
return func
return typeguard.typechecked(func, *args, **kwargs)


def check_type(*args, **kwargs):
if os.getenv("PYCARDANO_NO_TYPE_CHECK", "False").lower() in ("true", "1"):
return None
return typeguard.check_type(*args, **kwargs)
20 changes: 20 additions & 0 deletions test/pycardano/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import typeguard

from pycardano.types import typechecked, check_type


def test_types(monkeypatch):
monkeypatch.setenv("PYCARDANO_NO_TYPE_CHECK", "true")

assert typeguard.typechecked != typechecked
assert typeguard.check_type != check_type

@typechecked
def func1():
pass

@typechecked()
def func2():
pass

check_type()