Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
ca3e7616
Commit
ca3e7616
authored
8 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
matching sigs
parent
29e561f4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
satella/coding/typecheck.py
+36
-20
36 additions, 20 deletions
satella/coding/typecheck.py
tests/test_coding/test_debug.py
+12
-0
12 additions, 0 deletions
tests/test_coding/test_debug.py
with
48 additions
and
20 deletions
satella/coding/typecheck.py
+
36
−
20
View file @
ca3e7616
...
...
@@ -117,7 +117,11 @@ class CallSignature(object):
- kwargs_name (Union[str, None]) - name of kwargs argument, or None if not present
"""
def
count_required_positionals
(
self
):
return
len
((
a
for
a
in
self
.
pos_args
if
a
.
required
))
c
=
0
for
a
in
self
.
pos_args
:
if
a
.
required
:
c
+=
1
return
c
def
__eq__
(
self
,
other
):
"""
...
...
@@ -203,7 +207,35 @@ class CallSignature(object):
return
locals
def
is_match_amount
(
self
,
*
args
,
**
kwargs
):
"""
Would a function call with these arguments succeed, based solely on number and
"
keywordnessity
"
or parameters?
"""
if
len
(
args
)
>
len
(
self
.
pos_args
):
if
not
self
.
has_varargs
:
return
False
# *args expected
if
len
(
args
)
<
self
.
count_required_positionals
():
return
False
# Not enough posits
if
len
(
kwargs
)
>
0
and
not
self
.
has_kwargs
:
return
False
# kwargs expected
return
True
def
__typeinfo_to_tuple_of_types
(
typeinfo
):
if
typeinfo
is
None
:
return
(
type
(
None
),)
elif
typeinfo
==
int
and
six
.
PY2
:
return
six
.
integer_types
else
:
if
isinstance
(
typeinfo
,
(
tuple
,
list
)):
new_tup
=
[]
for
elem
in
typeinfo
:
new_tup
.
extend
(
__typeinfo_to_tuple_of_types
(
elem
))
return
tuple
(
new_tup
)
else
:
return
(
typeinfo
,)
def
typed
(
*
t_args
,
**
t_kwargs
):
"""
...
...
@@ -229,30 +261,14 @@ def typed(*t_args, **t_kwargs):
:param t_args:
:param t_kwargs:
:return:
"""
def
typeinfo_to_tuple_of_types
(
typeinfo
):
if
typeinfo
is
None
:
return
(
type
(
None
),
)
elif
typeinfo
==
int
and
six
.
PY2
:
return
six
.
integer_types
else
:
if
isinstance
(
typeinfo
,
(
tuple
,
list
)):
new_tup
=
[]
for
elem
in
typeinfo
:
new_tup
.
extend
(
typeinfo_to_tuple_of_types
(
elem
))
return
tuple
(
new_tup
)
else
:
return
(
typeinfo
,
)
t_args
=
[(
typeinfo_to_tuple_of_types
(
x
)
if
x
is
not
None
else
None
)
for
x
in
t_args
]
t_args
=
[(
__typeinfo_to_tuple_of_types
(
x
)
if
x
is
not
None
else
None
)
for
x
in
t_args
]
t_retarg
=
t_kwargs
.
get
(
'
returns
'
,
None
)
if
t_retarg
is
not
None
:
t_retarg
=
typeinfo_to_tuple_of_types
(
t_retarg
)
t_retarg
=
__
typeinfo_to_tuple_of_types
(
t_retarg
)
def
outer
(
fun
):
...
...
This diff is collapsed.
Click to expand it.
tests/test_coding/test_debug.py
+
12
−
0
View file @
ca3e7616
...
...
@@ -20,6 +20,18 @@ class TestTypecheck(unittest.TestCase):
sum
(
1
,
2
)
self
.
assertRaises
(
TypeError
,
lambda
:
sum2
(
2
,
3
))
def
test_tma
(
self
):
def
test
(
a
,
b
,
c
,
**
d
):
pass
cs
=
CallSignature
(
test
)
self
.
assertTrue
(
cs
.
is_match_amount
(
1
,
2
,
3
,
wtf
=
4
))
self
.
assertFalse
(
cs
.
is_match_amount
(
1
,
2
,
wtf
=
4
))
self
.
assertTrue
(
cs
.
is_match_amount
(
1
,
2
,
3
))
self
.
assertFalse
(
cs
.
is_match_amount
(
1
,
2
,
3
,
4
))
def
test_t1
(
self
):
@typed
(
int
,
float
,
six
.
text_type
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment