Skip to content

Commit

Permalink
feat: allow users disable runtime type checking. (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Apr 20, 2022
1 parent 3dec99b commit c0f6294
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ jobs:
- name: Test with pytest
run: poetry run pytest -v -rs tests --runslow --cov=./ --cov-report=xml

- name: Test with pytest (runtime type checking disabled)
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.7'
run: poetry run pytest -v -rs tests --runslow
env:
STELLAR_SDK_RUNTIME_TYPE_CHECKING: 0

- name: Upload coverage to Codecov
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.7'
uses: codecov/codecov-action@v1
Expand Down
11 changes: 7 additions & 4 deletions stellar_sdk/type_checked.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typeguard import T_CallableOrType
from typeguard import typechecked as _typechecked

_STELLAR_SDK_ENFORCE_TYPE_CHECK_FLAG: str = "STELLAR_SDK_ENFORCE_TYPE_CHECK"
_STELLAR_SDK_ENFORCE_TYPE_CHECK: bool = os.getenv(
_STELLAR_SDK_ENFORCE_TYPE_CHECK_FLAG, "False"
_STELLAR_SDK_RUNTIME_TYPE_CHECKING_FLAG: str = "STELLAR_SDK_RUNTIME_TYPE_CHECKING"
_STELLAR_SDK_RUNTIME_TYPE_CHECKING: bool = os.getenv(
_STELLAR_SDK_RUNTIME_TYPE_CHECKING_FLAG, "True"
).lower() in ("true", "1", "t")


Expand All @@ -23,4 +23,7 @@ def type_checked(func: T_CallableOrType) -> T_CallableOrType:
def type_checked(
func=None,
):
return _typechecked(func=func, always=_STELLAR_SDK_ENFORCE_TYPE_CHECK)
if _STELLAR_SDK_RUNTIME_TYPE_CHECKING:
return _typechecked(func=func)
else:
return func
7 changes: 6 additions & 1 deletion tests/operation/test_operation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from stellar_sdk import Operation
from stellar_sdk.type_checked import _STELLAR_SDK_RUNTIME_TYPE_CHECKING


class TestOperation:
Expand All @@ -19,10 +20,14 @@ def test_to_xdr_amount(self, origin_amount, expect_value):
@pytest.mark.parametrize(
"origin_amount, exception, reason",
[
(
pytest.param(
10,
TypeError,
'type of argument "value" must be one of \\(str, decimal.Decimal\\); got int instead',
marks=pytest.mark.skipif(
not _STELLAR_SDK_RUNTIME_TYPE_CHECKING,
reason="runtime_type_checking_disabled",
),
),
(
"-0.1",
Expand Down
4 changes: 4 additions & 0 deletions tests/sep/test_stellar_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
TransactionStellarUri,
)
from stellar_sdk.transaction_envelope import TransactionEnvelope
from stellar_sdk.type_checked import _STELLAR_SDK_RUNTIME_TYPE_CHECKING


class TestStellarTransactionStellarUri:
Expand Down Expand Up @@ -481,6 +482,9 @@ def test_message_too_long_raise(self):
message=message,
)

@pytest.mark.skipif(
not _STELLAR_SDK_RUNTIME_TYPE_CHECKING, reason="runtime_type_checking_disabled"
)
def test_invalid_memo_raise(self):
memo = "invalid memo"
with pytest.raises(
Expand Down
4 changes: 4 additions & 0 deletions tests/sep/test_stellar_web_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from stellar_sdk.transaction_builder import TransactionBuilder
from stellar_sdk.transaction_envelope import TransactionEnvelope
from stellar_sdk.type_checked import _STELLAR_SDK_RUNTIME_TYPE_CHECKING


class TestStellarWebAuthentication:
Expand Down Expand Up @@ -117,6 +118,9 @@ def test_challenge_transaction_id_memo_as_int_permitted(self):
).transaction
assert transaction.memo == IdMemo(memo)

@pytest.mark.skipif(
not _STELLAR_SDK_RUNTIME_TYPE_CHECKING, reason="runtime_type_checking_disabled"
)
def test_challenge_transaction_non_id_memo_not_permitted(self):
server_kp = Keypair.random()
client_account_id = Keypair.random().public_key
Expand Down

0 comments on commit c0f6294

Please sign in to comment.