Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
d008de94
Commit
d008de94
authored
Jul 01, 2018
by
eckhart
Browse files
- move issubtype and isgenerictype from transform.py to toolkit.py
parent
ac43cdc4
Changes
3
Hide whitespace changes
Inline
Side-by-side
DHParser/toolkit.py
View file @
d008de94
...
...
@@ -24,6 +24,7 @@ so that they are best defined in a toolkit-module.
import
codecs
import
hashlib
import
inspect
import
io
import
parser
...
...
@@ -45,6 +46,8 @@ __all__ = ('escape_re',
'escape_control_characters'
,
'is_filename'
,
'lstrip_docstring'
,
'issubtype'
,
'isgenerictype'
,
'load_if_file'
,
'is_python_code'
,
'md5'
,
...
...
@@ -101,6 +104,34 @@ def is_filename(strg: str) -> bool:
# and strg.select('*') < 0 and strg.select('?') < 0
#######################################################################
#
# type system support
#
#######################################################################
def
issubtype
(
sub_type
,
base_type
):
if
sys
.
version_info
.
major
<=
3
and
sys
.
version_info
.
minor
<=
6
:
return
issubclass
(
sub_type
,
base_type
)
try
:
base_type
=
base_type
.
__origin__
except
AttributeError
:
pass
try
:
mro
=
inspect
.
getmro
(
sub_type
)
except
AttributeError
:
mro
=
[]
for
t
in
sub_type
.
__mro_entries__
([
sub_type
]):
mro
.
extend
(
inspect
.
getmro
(
t
))
# print(" " if base_type in mro else "!", base_type, sub_type, mro)
return
base_type
in
mro
def
isgenerictype
(
t
):
return
str
(
t
).
endswith
(
']'
)
#######################################################################
#
# loading and compiling
...
...
DHParser/transform.py
View file @
d008de94
...
...
@@ -33,7 +33,7 @@ from functools import partial, reduce, singledispatch
from
DHParser.error
import
Error
from
DHParser.syntaxtree
import
Node
,
WHITESPACE_PTYPE
,
TOKEN_PTYPE
,
MockParser
,
ZOMBIE_NODE
from
DHParser.toolkit
import
expand_table
,
smart_list
,
re
,
typing
from
DHParser.toolkit
import
issubtype
,
isgenerictype
,
expand_table
,
smart_list
,
re
,
typing
from
typing
import
AbstractSet
,
Any
,
ByteString
,
Callable
,
cast
,
Container
,
Dict
,
\
Tuple
,
List
,
Sequence
,
Union
,
Text
,
Generic
...
...
@@ -150,29 +150,6 @@ def transformation_factory(t1=None, t2=None, t3=None, t4=None, t5=None):
does not have type annotations.
"""
def
issubtype
(
sub_type
,
base_type
):
# Because of changes in thy typing system, the following doesn't
# work any more in Python 3.7:
#
# return base_type in inspect.getmro(subtype)
#
# Therefore it has all gotten a bit more complicated...
try
:
base_type
=
base_type
.
__origin__
except
AttributeError
:
pass
try
:
mro
=
inspect
.
getmro
(
sub_type
)
except
AttributeError
:
mro
=
[]
for
t
in
sub_type
.
__mro_entries__
([
sub_type
]):
mro
.
extend
(
inspect
.
getmro
(
t
))
print
(
" "
if
base_type
in
mro
else
"!"
,
base_type
,
sub_type
,
mro
)
return
base_type
in
mro
def
isgenerictype
(
t
):
return
str
(
t
).
endswith
(
']'
)
def
type_guard
(
t
):
"""Raises an error if type `t` is a generic type or could be mistaken
for the type of the canonical first parameter "List[Node] of
...
...
test/test_toolkit.py
View file @
d008de94
...
...
@@ -21,13 +21,14 @@ limitations under the License.
"""
import
concurrent.futures
import
collections.abc
import
os
import
sys
sys
.
path
.
extend
([
'../'
,
'./'
])
from
DHParser.toolkit
import
has_fenced_code
,
load_if_file
,
re
,
\
lstrip_docstring
lstrip_docstring
,
issubtype
,
isgenerictype
,
typing
from
DHParser.log
import
log_dir
,
logging
,
is_logging
...
...
@@ -150,6 +151,12 @@ class TestStringHelpers:
'indented line'
class
TestTypeSystemSupport
:
def
test_issubtype
(
self
):
assert
issubtype
(
typing
.
List
,
typing
.
List
)
if
__name__
==
"__main__"
:
from
DHParser.testing
import
runner
runner
(
""
,
globals
())
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment