Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gboml
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
smart_grids
public
gboml
Commits
64ceee4b
Commit
64ceee4b
authored
2 years ago
by
Derval Guillaume
Browse files
Options
Downloads
Patches
Plain Diff
Check parsing results
parent
0261aa86
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/gboml/ast/check.py
+62
-0
62 additions, 0 deletions
src/gboml/ast/check.py
tests/test_parsing.py
+2
-1
2 additions, 1 deletion
tests/test_parsing.py
with
64 additions
and
1 deletion
src/gboml/ast/check.py
0 → 100644
+
62
−
0
View file @
64ceee4b
import
types
import
typing
from
dataclasses
import
is_dataclass
,
fields
from
gboml.ast
import
*
class
NotOfType
(
Exception
):
pass
class
InvalidContent
(
Exception
):
pass
def
_check_type
(
value
,
typ
):
if
isinstance
(
typ
,
str
):
typ
=
eval
(
typ
)
if
isinstance
(
typ
,
types
.
GenericAlias
)
and
typ
.
__origin__
==
list
:
subtyp
=
typing
.
get_args
(
typ
)[
0
]
if
not
isinstance
(
value
,
list
):
raise
NotOfType
(
f
"
{
value
}
is not an instance of
{
typ
}
"
)
for
x
in
value
:
_check_type
(
x
,
subtyp
)
return
if
isinstance
(
typ
,
types
.
UnionType
):
for
subtyp
in
typing
.
get_args
(
typ
):
try
:
_check_type
(
value
,
subtyp
)
return
except
NotOfType
as
e
:
pass
raise
NotOfType
(
f
"
{
value
}
is not an instance of
{
typ
}
"
)
if
typ
==
typing
.
Optional
[
typing
.
Any
]:
return
if
typ
is
typing
.
Any
:
if
typ
is
not
None
:
return
raise
NotOfType
(
f
"
{
value
}
is not an instance of
{
typ
}
"
)
if
is_dataclass
(
typ
):
if
not
isinstance
(
value
,
typ
):
raise
NotOfType
(
f
"
{
value
}
is not an instance of
{
typ
}
"
)
check
(
value
)
return
try
:
if
not
isinstance
(
value
,
typ
):
raise
NotOfType
(
f
"
{
value
}
is not an instance of
{
typ
}
"
)
except
TypeError
:
print
(
typ
)
return
def
check
(
t
:
GBOMLObject
):
"""
Check that a GBOMLObject is well-formed
"""
try
:
for
f
in
fields
(
t
):
value
=
getattr
(
t
,
f
.
name
)
typ
=
f
.
type
_check_type
(
value
,
typ
)
except
NotOfType
as
e
:
raise
InvalidContent
(
str
(
e
)
+
f
"
in
{
t
}
"
)
This diff is collapsed.
Click to expand it.
tests/test_parsing.py
+
2
−
1
View file @
64ceee4b
import
unittest
from
pathlib
import
Path
from
gboml.ast.check
import
check
from
gboml.parsing
import
parse_file
instance_dir
=
Path
(
__file__
).
parent
/
"
instances
"
...
...
@@ -24,7 +25,7 @@ class TestParsing(unittest.TestCase):
"""
Checks if the file (doesn
'
t) parses
"""
for
filename
in
should_pass
:
with
self
.
subTest
(
filename
=
filename
):
parse_file
(
str
(
filename
))
check
(
parse_file
(
str
(
filename
))
)
@unittest.expectedFailure
def
test_not_parse
(
self
):
...
...
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