]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.dap/type_check.py
1 # Copyright 2023-2024 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Test the type checker.
19 from gdb
.dap
.typecheck
import type_check
22 # A wrapper to call a function that should cause a type-checking
23 # failure. Returns if the exception was seen. Throws an exception if
24 # a TypeError was not seen.
25 def should_fail(func
, **args
):
30 raise RuntimeError("function failed to throw TypeError")
33 # Also specify a return type to make sure return types do not confuse
36 def simple_types(*, b
: bool, s
: str, i
: int = 23) -> int:
41 simple_types(b
=True, s
="DEI", i
=97)
42 # Check the absence of a defaulted argument.
43 simple_types(b
=True, s
="DEI")
44 simple_types(b
=False, s
="DEI", i
=97)
45 should_fail(simple_types
, b
=97, s
="DEI", i
=97)
46 should_fail(simple_types
, b
=True, s
=None, i
=97)
47 should_fail(simple_types
, b
=True, s
="DEI", i
={})
51 def sequence_type(*, s
: typing
.Sequence
[str]):
56 sequence_type(s
=("hi", "out", "there"))
57 sequence_type(s
=["hi", "out", "there"])
60 should_fail(sequence_type
, s
=23)
61 should_fail(sequence_type
, s
=["hi", 97])
65 def map_type(*, m
: typing
.Mapping
[str, int]):
71 map_type(m
={"dei": 23})
72 should_fail(map_type
, m
=[1, 2, 3])
73 should_fail(map_type
, m
=None)
74 should_fail(map_type
, m
={"dei": "string"})
78 def opt_type(*, u
: typing
.Optional
[int]):
85 should_fail(opt_type
, u
="string")
88 def check_everything():
89 # Older versions of Python can't really implement this.
90 if hasattr(typing
, "get_origin"):
91 # Just let any exception propagate and end up in the log.