Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils-sfg
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
pycodegen
pystencils-sfg
Commits
7d4f72c5
Commit
7d4f72c5
authored
5 months ago
by
Frederik Hennig
Browse files
Options
Downloads
Patches
Plain Diff
simplify type class factory as a function
parent
80f224a1
No related branches found
No related tags found
1 merge request
!12
Improve versatility and robustness of `cpptype`, and document it in the user guide
Pipeline
#71534
failed
5 months ago
Stage: Code Quality
Stage: Tests
Stage: Documentation
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/pystencilssfg/lang/types.py
+9
-10
9 additions, 10 deletions
src/pystencilssfg/lang/types.py
tests/lang/test_types.py
+28
-30
28 additions, 30 deletions
tests/lang/test_types.py
with
37 additions
and
40 deletions
src/pystencilssfg/lang/types.py
+
9
−
10
View file @
7d4f72c5
from
__future__
import
annotations
from
__future__
import
annotations
from
typing
import
Any
,
Iterable
,
Sequence
,
Mapping
from
typing
import
Any
,
Iterable
,
Sequence
,
Mapping
,
Callable
from
abc
import
ABC
from
abc
import
ABC
from
dataclasses
import
dataclass
from
dataclasses
import
dataclass
from
itertools
import
chain
from
itertools
import
chain
...
@@ -107,7 +107,7 @@ class CppType(PsCustomType, ABC):
...
@@ -107,7 +107,7 @@ class CppType(PsCustomType, ABC):
def
cpptype
(
def
cpptype
(
typestr
:
str
,
include
:
str
|
HeaderFile
|
Iterable
[
str
|
HeaderFile
]
=
()
typestr
:
str
,
include
:
str
|
HeaderFile
|
Iterable
[
str
|
HeaderFile
]
=
()
)
->
type
[
CppType
]:
)
->
Callable
[...,
CppType
|
Ref
]:
headers
:
list
[
str
|
HeaderFile
]
headers
:
list
[
str
|
HeaderFile
]
if
isinstance
(
include
,
(
str
,
HeaderFile
)):
if
isinstance
(
include
,
(
str
,
HeaderFile
)):
...
@@ -121,15 +121,14 @@ def cpptype(
...
@@ -121,15 +121,14 @@ def cpptype(
template_string
=
typestr
template_string
=
typestr
class_includes
=
frozenset
(
HeaderFile
.
parse
(
h
)
for
h
in
headers
)
class_includes
=
frozenset
(
HeaderFile
.
parse
(
h
)
for
h
in
headers
)
class
TypeClassFactory
(
TypeClass
):
def
factory
(
*
args
,
ref
:
bool
=
False
,
**
kwargs
):
def
__new__
(
cls
,
*
args
,
ref
:
bool
=
False
,
**
kwargs
):
obj
=
TypeClass
(
*
args
,
**
kwargs
)
obj
=
TypeClass
(
*
args
,
**
kwargs
)
if
ref
:
if
ref
:
return
Ref
(
obj
)
return
Ref
(
obj
)
else
:
else
:
return
obj
return
obj
return
TypeClassF
actory
return
staticmethod
(
f
actory
)
class
Ref
(
PsType
):
class
Ref
(
PsType
):
...
...
This diff is collapsed.
Click to expand it.
tests/lang/test_types.py
+
28
−
30
View file @
7d4f72c5
...
@@ -6,13 +6,12 @@ from pystencils.types import constify, deconstify
...
@@ -6,13 +6,12 @@ from pystencils.types import constify, deconstify
def
test_cpptypes
():
def
test_cpptypes
():
t
class
=
cpptype
(
"
std::vector< {}, {} >
"
,
"
<vector>
"
)
t
factory
=
cpptype
(
"
std::vector< {}, {} >
"
,
"
<vector>
"
)
vec_type
=
t
class
(
create_type
(
"
float32
"
),
"
std::allocator< float >
"
)
vec_type
=
t
factory
(
create_type
(
"
float32
"
),
"
std::allocator< float >
"
)
assert
str
(
vec_type
).
strip
()
==
"
std::vector< float, std::allocator< float > >
"
assert
str
(
vec_type
).
strip
()
==
"
std::vector< float, std::allocator< float > >
"
assert
(
assert
(
tclass
.
class_includes
vec_type
.
includes
==
vec_type
.
includes
==
{
HeaderFile
(
"
vector
"
,
system_header
=
True
)}
==
{
HeaderFile
(
"
vector
"
,
system_header
=
True
)}
)
)
...
@@ -20,60 +19,59 @@ def test_cpptypes():
...
@@ -20,60 +19,59 @@ def test_cpptypes():
assert
deconstify
(
constify
(
vec_type
))
==
vec_type
assert
deconstify
(
constify
(
vec_type
))
==
vec_type
# Duplicate Equality
# Duplicate Equality
assert
t
class
(
create_type
(
"
float32
"
),
"
std::allocator< float >
"
)
==
vec_type
assert
t
factory
(
create_type
(
"
float32
"
),
"
std::allocator< float >
"
)
==
vec_type
# Not equal with different argument even though it produces the same string
# Not equal with different argument even though it produces the same string
assert
t
class
(
"
float
"
,
"
std::allocator< float >
"
)
!=
vec_type
assert
t
factory
(
"
float
"
,
"
std::allocator< float >
"
)
!=
vec_type
# The same with keyword arguments
# The same with keyword arguments
t
class
=
cpptype
(
"
std::vector< {T}, {Allocator} >
"
,
"
<vector>
"
)
t
factory
=
cpptype
(
"
std::vector< {T}, {Allocator} >
"
,
"
<vector>
"
)
vec_type
=
t
class
(
T
=
create_type
(
"
float32
"
),
Allocator
=
"
std::allocator< float >
"
)
vec_type
=
t
factory
(
T
=
create_type
(
"
float32
"
),
Allocator
=
"
std::allocator< float >
"
)
assert
str
(
vec_type
).
strip
()
==
"
std::vector< float, std::allocator< float > >
"
assert
str
(
vec_type
).
strip
()
==
"
std::vector< float, std::allocator< float > >
"
assert
deconstify
(
constify
(
vec_type
))
==
vec_type
assert
deconstify
(
constify
(
vec_type
))
==
vec_type
def
test_cpptype_invalid_construction
():
def
test_cpptype_invalid_construction
():
t
class
=
cpptype
(
"
std::vector< {}, {Allocator} >
"
,
"
<vector>
"
)
t
factory
=
cpptype
(
"
std::vector< {}, {Allocator} >
"
,
"
<vector>
"
)
with
pytest
.
raises
(
IndexError
):
with
pytest
.
raises
(
IndexError
):
_
=
t
class
(
Allocator
=
"
SomeAlloc
"
)
_
=
t
factory
(
Allocator
=
"
SomeAlloc
"
)
with
pytest
.
raises
(
KeyError
):
with
pytest
.
raises
(
KeyError
):
_
=
t
class
(
"
int
"
)
_
=
t
factory
(
"
int
"
)
with
pytest
.
raises
(
ValueError
,
match
=
"
Too many positional arguments
"
):
with
pytest
.
raises
(
ValueError
,
match
=
"
Too many positional arguments
"
):
_
=
t
class
(
"
int
"
,
"
bogus
"
,
Allocator
=
"
SomeAlloc
"
)
_
=
t
factory
(
"
int
"
,
"
bogus
"
,
Allocator
=
"
SomeAlloc
"
)
with
pytest
.
raises
(
ValueError
,
match
=
"
Extraneous keyword arguments
"
):
with
pytest
.
raises
(
ValueError
,
match
=
"
Extraneous keyword arguments
"
):
_
=
t
class
(
"
int
"
,
Allocator
=
"
SomeAlloc
"
,
bogus
=
2
)
_
=
t
factory
(
"
int
"
,
Allocator
=
"
SomeAlloc
"
,
bogus
=
2
)
def
test_cpptype_const
():
def
test_cpptype_const
():
t
class
=
cpptype
(
"
std::vector< {T} >
"
,
"
<vector>
"
)
t
factory
=
cpptype
(
"
std::vector< {T} >
"
,
"
<vector>
"
)
vec_type
=
t
class
(
T
=
create_type
(
"
uint32
"
))
vec_type
=
t
factory
(
T
=
create_type
(
"
uint32
"
))
assert
constify
(
vec_type
)
==
t
class
(
T
=
create_type
(
"
uint32
"
),
const
=
True
)
assert
constify
(
vec_type
)
==
t
factory
(
T
=
create_type
(
"
uint32
"
),
const
=
True
)
vec_type
=
t
class
(
T
=
create_type
(
"
uint32
"
),
const
=
True
)
vec_type
=
t
factory
(
T
=
create_type
(
"
uint32
"
),
const
=
True
)
assert
deconstify
(
vec_type
)
==
t
class
(
T
=
create_type
(
"
uint32
"
),
const
=
False
)
assert
deconstify
(
vec_type
)
==
t
factory
(
T
=
create_type
(
"
uint32
"
),
const
=
False
)
def
test_cpptype_ref
():
def
test_cpptype_ref
():
t
class
=
cpptype
(
"
std::vector< {T} >
"
,
"
<vector>
"
)
t
factory
=
cpptype
(
"
std::vector< {T} >
"
,
"
<vector>
"
)
vec_type
=
t
class
(
T
=
create_type
(
"
uint32
"
),
ref
=
True
)
vec_type
=
t
factory
(
T
=
create_type
(
"
uint32
"
),
ref
=
True
)
assert
isinstance
(
vec_type
,
Ref
)
assert
isinstance
(
vec_type
,
Ref
)
assert
strip_ptr_ref
(
vec_type
)
==
t
class
(
T
=
create_type
(
"
uint32
"
))
assert
strip_ptr_ref
(
vec_type
)
==
t
factory
(
T
=
create_type
(
"
uint32
"
))
def
test_cpptype_inherits_headers
():
def
test_cpptype_inherits_headers
():
optional_tclass
=
cpptype
(
"
std::optional< {T} >
"
,
"
<optional>
"
)
optional_tfactory
=
cpptype
(
"
std::optional< {T} >
"
,
"
<optional>
"
)
vec_tclass
=
cpptype
(
"
std::vector< {T} >
"
,
"
<vector>
"
)
vec_tfactory
=
cpptype
(
"
std::vector< {T} >
"
,
"
<vector>
"
)
vec_type
=
vec_tclass
(
T
=
optional_tclass
(
T
=
"
int
"
))
vec_type
=
vec_tfactory
(
T
=
optional_tfactory
(
T
=
"
int
"
))
assert
(
assert
vec_type
.
includes
==
{
vec_type
.
includes
HeaderFile
.
parse
(
"
<optional>
"
),
==
optional_tclass
.
class_includes
|
vec_tclass
.
class_includes
HeaderFile
.
parse
(
"
<vector>
"
),
==
{
HeaderFile
.
parse
(
"
<optional>
"
),
HeaderFile
.
parse
(
"
<vector>
"
)}
}
)
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