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
27862e6e
Commit
27862e6e
authored
7 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
shorter lines
parent
54af503c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
satella/coding/typecheck.py
+22
-15
22 additions, 15 deletions
satella/coding/typecheck.py
with
22 additions
and
15 deletions
satella/coding/typecheck.py
+
22
−
15
View file @
27862e6e
...
@@ -60,7 +60,9 @@ class CSArgument(_CSArgument):
...
@@ -60,7 +60,9 @@ class CSArgument(_CSArgument):
:param other: CSArgument
:param other: CSArgument
:return: bool
:return: bool
"""
"""
return
self
.
name
==
other
.
name
and
self
.
required
==
other
.
required
and
self
.
default_value
==
other
.
default_value
return
self
.
name
==
other
.
name
and
\
self
.
required
==
other
.
required
and
\
self
.
default_value
==
other
.
default_value
class
CSVarargsPlaceholder
(
CSArgument
):
class
CSVarargsPlaceholder
(
CSArgument
):
...
@@ -100,7 +102,8 @@ class CSBadTypeError(CSTypeError):
...
@@ -100,7 +102,8 @@ class CSBadTypeError(CSTypeError):
self
.
got
=
got
self
.
got
=
got
def
__str__
(
self
):
def
__str__
(
self
):
return
'
Bad type given for arg %s, expected %s got %s
'
%
(
self
.
arg
.
name
,
self
.
expected
,
self
.
got
)
return
'
Bad type given for arg %s, expected %s got %s
'
%
(
self
.
arg
.
name
,
self
.
expected
,
self
.
got
)
class
CSNotGivenError
(
CSTypeError
):
class
CSNotGivenError
(
CSTypeError
):
...
@@ -143,7 +146,8 @@ class CallSignature(object):
...
@@ -143,7 +146,8 @@ class CallSignature(object):
if
any
(
a
!=
b
for
a
,
b
in
zip
(
self
.
pos_args
,
other
.
pos_args
)):
if
any
(
a
!=
b
for
a
,
b
in
zip
(
self
.
pos_args
,
other
.
pos_args
)):
return
False
return
False
return
(
self
.
has_kwargs
==
other
.
has_kwargs
)
and
(
self
.
has_varargs
==
other
.
has_varargs
)
return
self
.
has_kwargs
==
other
.
has_kwargs
and
\
self
.
has_varargs
==
other
.
has_varargs
def
__init__
(
self
,
callable
):
def
__init__
(
self
,
callable
):
args
,
varargs
,
kwargs
,
defaults
=
inspect
.
getargspec
(
callable
)
args
,
varargs
,
kwargs
,
defaults
=
inspect
.
getargspec
(
callable
)
...
@@ -166,14 +170,16 @@ class CallSignature(object):
...
@@ -166,14 +170,16 @@ class CallSignature(object):
self
.
varargs_name
=
varargs
self
.
varargs_name
=
varargs
if
varargs
is
not
None
:
if
varargs
is
not
None
:
self
.
has_varargs
=
True
self
.
has_varargs
=
True
self
.
locals
[
self
.
varargs_name
]
=
CSVarargsPlaceholder
(
self
.
varargs_name
,
False
,
[])
self
.
locals
[
self
.
varargs_name
]
=
CSVarargsPlaceholder
(
self
.
varargs_name
,
False
,
[])
else
:
else
:
self
.
has_varargs
=
False
self
.
has_varargs
=
False
self
.
kwargs_name
=
kwargs
self
.
kwargs_name
=
kwargs
if
kwargs
is
not
None
:
if
kwargs
is
not
None
:
self
.
has_kwargs
=
True
self
.
has_kwargs
=
True
self
.
locals
[
self
.
kwargs_name
]
=
CSKwargsPlaceholder
(
self
.
kwargs_name
,
False
,
{})
self
.
locals
[
self
.
kwargs_name
]
=
CSKwargsPlaceholder
(
self
.
kwargs_name
,
False
,
{})
else
:
else
:
self
.
has_kwargs
=
False
self
.
has_kwargs
=
False
...
@@ -236,7 +242,7 @@ class CallSignature(object):
...
@@ -236,7 +242,7 @@ class CallSignature(object):
def
__typeinfo_to_tuple_of_types
(
typeinfo
):
def
__typeinfo_to_tuple_of_types
(
typeinfo
):
if
typeinfo
==
'
self
'
:
if
typeinfo
==
'
self
'
:
return
None
return
None
if
typeinfo
is
None
:
el
if
typeinfo
is
None
:
return
(
type
(
None
),)
return
(
type
(
None
),)
elif
typeinfo
==
int
and
six
.
PY2
:
elif
typeinfo
==
int
and
six
.
PY2
:
return
six
.
integer_types
return
six
.
integer_types
...
@@ -269,21 +275,22 @@ def typed(*t_args, **t_kwargs):
...
@@ -269,21 +275,22 @@ def typed(*t_args, **t_kwargs):
def method(self, a, b):
def method(self, a, b):
..
..
If you specify extra argument - mandatory=True - type will always be
checked,
If you specify extra argument - mandatory=True - type will always be
regardless if debug mode is enabled
checked,
regardless if debug mode is enabled
Same rules apply.
Same rules apply.
int will automatically include long for checking (Python 3 compatibility)
int will automatically include long for checking (Python 3 compatibility)
If you want to check for None, type (None, )
If you want to check for None, type (None, )
None for an argument means
"
do no checking
"
, (None, ) means
"
type must be
NoneType
"
None for an argument means
"
do no checking
"
, (None, ) means
"
type must be
You can pass tuples or lists to match for multiple types
NoneType
"
.
You can pass tuples or lists to match for multiple types
:param t_args:
:param t_args:
:param t_kwargs:
:param t_kwargs:
"""
"""
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
)
t_retarg
=
t_kwargs
.
get
(
'
returns
'
,
None
)
is_mandatory
=
t_kwargs
.
get
(
'
mandatory
'
,
False
)
is_mandatory
=
t_kwargs
.
get
(
'
mandatory
'
,
False
)
...
@@ -301,16 +308,16 @@ def typed(*t_args, **t_kwargs):
...
@@ -301,16 +308,16 @@ def typed(*t_args, **t_kwargs):
for
argument
,
typedescr
in
zip
(
args
,
t_args
):
for
argument
,
typedescr
in
zip
(
args
,
t_args
):
if
typedescr
is
not
None
:
if
typedescr
is
not
None
:
if
not
isinstance
(
argument
,
typedescr
):
if
not
isinstance
(
argument
,
typedescr
):
raise
TypeError
(
'
Got %s, expected %s
'
%
(
type
(
argument
),
typedescr
))
raise
TypeError
(
'
Got %s, expected %s
'
%
(
type
(
argument
),
typedescr
))
rt
=
fun
(
*
args
,
**
kwargs
)
rt
=
fun
(
*
args
,
**
kwargs
)
if
t_retarg
is
not
None
:
if
t_retarg
is
not
None
:
if
not
isinstance
(
rt
,
t_retarg
):
if
not
isinstance
(
rt
,
t_retarg
):
raise
TypeError
(
'
Returned %s, expected %s
'
%
(
type
(
rt
),
t_retarg
))
raise
TypeError
(
'
Returned %s, expected %s
'
%
(
type
(
rt
),
t_retarg
))
return
rt
return
rt
return
inner
return
inner
return
outer
return
outer
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