Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
e93c2c55
Commit
e93c2c55
authored
May 15, 2021
by
matsjoyce
Committed by
GitHub
May 15, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix annotations for decorated classes (GH-4151)
parent
fa922c9d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
71 additions
and
2 deletions
+71
-2
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+2
-2
runtests.py
runtests.py
+1
-0
tests/run/pep557_dataclasses.py
tests/run/pep557_dataclasses.py
+54
-0
tests/run/pep563_annotations.py
tests/run/pep563_annotations.py
+14
-0
No files found.
Cython/Compiler/Nodes.py
View file @
e93c2c55
...
...
@@ -4878,7 +4878,7 @@ class PyClassDefNode(ClassDefNode):
return
cenv
def
analyse_declarations
(
self
,
env
):
class_result
=
self
.
classobj
unwrapped_class_result
=
class_result
=
self
.
classobj
if
self
.
decorators
:
from
.ExprNodes
import
SimpleCallNode
for
decorator
in
self
.
decorators
[::
-
1
]:
...
...
@@ -4900,7 +4900,7 @@ class PyClassDefNode(ClassDefNode):
if
self
.
doc_node
:
self
.
doc_node
.
analyse_target_declaration
(
cenv
)
self
.
body
.
analyse_declarations
(
cenv
)
self
.
class_result
.
analyse_annotations
(
cenv
)
unwrapped_
class_result
.
analyse_annotations
(
cenv
)
update_bases_functype
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
py_object_type
,
[
...
...
runtests.py
View file @
e93c2c55
...
...
@@ -443,6 +443,7 @@ VER_DEP_MODULES = {
'run.time_pxd', # _PyTime_GetSystemClock doesn't exist in 3.4
]),
(3,7): (operator.lt, lambda x: x in ['run.pycontextvar',
'run.pep557_dataclasses', # dataclasses module
]),
}
...
...
tests/run/pep557_dataclasses.py
0 → 100644
View file @
e93c2c55
# mode: run
# tag: pep557, pure3.7
import
dataclasses
from
typing
import
Sequence
@
dataclasses
.
dataclass
class
Color
:
"""
>>> list(Color.__dataclass_fields__.keys())
['red', 'green', 'blue', 'alpha']
>>> Color(1, 2, 3)
Color(red=1, green=2, blue=3, alpha=255)
>>> Color(1, 2, 3, 4)
Color(red=1, green=2, blue=3, alpha=4)
>>> Color(green=1, blue=2, red=3, alpha=40)
Color(red=3, green=1, blue=2, alpha=40)
"""
red
:
int
green
:
int
blue
:
int
alpha
:
int
=
255
@
dataclasses
.
dataclass
class
NamedColor
(
Color
):
"""
>>> list(NamedColor.__dataclass_fields__.keys())
['red', 'green', 'blue', 'alpha', 'names']
>>> NamedColor(1, 2, 3)
NamedColor(red=1, green=2, blue=3, alpha=255, names=[])
>>> NamedColor(1, 2, 3, 4)
NamedColor(red=1, green=2, blue=3, alpha=4, names=[])
>>> NamedColor(green=1, blue=2, red=3, alpha=40)
NamedColor(red=3, green=1, blue=2, alpha=40, names=[])
>>> NamedColor(1, 2, 3, names=["blackish", "very dark cyan"])
NamedColor(red=1, green=2, blue=3, alpha=255, names=['blackish', 'very dark cyan'])
"""
names
:
Sequence
[
str
]
=
dataclasses
.
field
(
default_factory
=
list
)
@
dataclasses
.
dataclass
(
frozen
=
True
)
class
IceCream
:
"""
>>> IceCream("vanilla")
IceCream(flavour='vanilla', num_toppings=2)
>>> IceCream("vanilla") == IceCream("vanilla", num_toppings=3)
False
>>> IceCream("vanilla") == IceCream("vanilla", num_toppings=2)
True
"""
flavour
:
str
num_toppings
:
int
=
2
tests/run/pep563_annotations.py
View file @
e93c2c55
...
...
@@ -24,3 +24,17 @@ def f(a: 1+2==3, b: list, c: this_cant_evaluate, d: "Hello from inside a string"
True
"""
pass
def
empty_decorator
(
cls
):
return
cls
@
empty_decorator
class
DecoratedStarship
(
object
):
"""
>>> sorted(DecoratedStarship.__annotations__.items())
[('captain', 'str'), ('damage', 'cython.int')]
"""
captain
:
str
=
'Picard'
# instance variable with default
damage
:
cython
.
int
# instance variable without default
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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