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
15cbfac6
You need to sign in or sign up before continuing.
Commit
15cbfac6
authored
1 year ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
fixes
parent
cf82491c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
satella/coding/overloading.py
+14
-6
14 additions, 6 deletions
satella/coding/overloading.py
with
14 additions
and
6 deletions
satella/coding/overloading.py
+
14
−
6
View file @
15cbfac6
...
...
@@ -6,6 +6,8 @@ import typing as tp
# Taken from https://stackoverflow.com/questions/28237955/same-name-for-classmethod-and-
# instancemethod
class
class_or_instancemethod
(
classmethod
):
"""
A decorator to make your methods both classmethods (they will receive an instance of type
...
...
@@ -110,6 +112,8 @@ class overload:
>>>
@what_type.overload
>>>
def
what_type
(
x
:
int
):
>>>
print
(
'
Int
'
)
>>>
what_type
(
5
)
>>>
what_type
(
'
string
'
)
Note that this instance
'
s __wrapped__ will refer to the first function.
TypeError will be called if no signatures match arguments.
...
...
@@ -120,7 +124,11 @@ class overload:
if
hasattr
(
fun
,
'
__doc__
'
):
self
.
__doc__
=
fun
.
__doc__
self
.
__wrapped__
=
fun
self
.
history_list
=
[]
@property
def
all_functions
(
self
)
->
tp
.
Iterable
[
object
]:
"""
Return a list of all functions registered within this overload
"""
return
self
.
type_signatures_to_functions
.
values
()
def
overload
(
self
,
fun
):
"""
...
...
@@ -138,12 +146,12 @@ class overload:
:raises TypeError: no type signature given
"""
matching
s
=
[]
matching
=
[]
for
sign
,
fun
in
self
.
type_signatures_to_functions
.
items
():
if
sign
.
matches
(
*
args
,
**
kwargs
):
matching
s
.
append
((
sign
,
fun
))
matching
s
.
sort
()
if
not
matching
s
:
matching
.
append
((
sign
,
fun
))
matching
.
sort
()
if
not
matching
:
raise
TypeError
(
'
No matching entries!
'
)
else
:
return
matching
s
[
-
1
][
1
](
*
args
,
**
kwargs
)
# call the most specific function you could find
return
matching
[
-
1
][
1
](
*
args
,
**
kwargs
)
# call the most specific function you could find
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