Skip to content

Commit

Permalink
Fixed error message when checking against a Union[..., Literal[...]]
Browse files Browse the repository at this point in the history
Fixes #113.
  • Loading branch information
agronholm committed Jun 2, 2020
1 parent f8f7fd8 commit c79bd2e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-v
- Added support for ``typing_extensions.Literal`` (PR by Ryan Rowe)
- Fixed unintended wrapping of untyped generators (PR by prescod)
- Fixed checking against bound type variables with ``check_type()`` without a call memo
- Fixed error message when checking against a ``Union`` containing a ``Literal``

**2.7.1** (2019-12-27)

Expand Down
12 changes: 11 additions & 1 deletion tests/test_typeguard_py38.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, TypedDict
from typing import Literal, TypedDict, Union

import pytest

Expand All @@ -14,6 +14,16 @@ def foo(a: Literal[1, 6, 8]):
pytest.raises(TypeError, foo, 4).match(r'must be one of \(1, 6, 8\); got 4 instead$')


def test_literal_union():
@typechecked
def foo(a: Union[str, Literal[1, 6, 8]]):
pass

foo(6)
pytest.raises(TypeError, foo, 4).\
match(r'must be one of \(str, typing.Literal\[1, 6, 8\]\); got int instead$')


@pytest.mark.parametrize('value, total, error_re', [
({'x': 6, 'y': 'foo'}, True, None),
({'y': 'foo'}, True, r'required key\(s\) \("x"\) missing from argument "arg"'),
Expand Down
2 changes: 1 addition & 1 deletion typeguard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def resolve_forwardref(maybe_ref, memo: _CallMemo):

def get_type_name(type_):
# typing.* types don't have a __name__ on Python 3.7+
return getattr(type_, '__name__', None) or type_._name
return getattr(type_, '__name__', None) or type_._name or str(type_)


def find_function(frame) -> Optional[Callable]:
Expand Down

0 comments on commit c79bd2e

Please sign in to comment.