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
dfd73ac6
Commit
dfd73ac6
authored
Nov 10, 2017
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Do not infer C types for arguments with Python default value.
Closes #1972.
parent
8ad16fc8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
2 deletions
+29
-2
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+12
-2
tests/run/annotation_typing.pyx
tests/run/annotation_typing.pyx
+17
-0
No files found.
Cython/Compiler/Nodes.py
View file @
dfd73ac6
...
...
@@ -68,8 +68,9 @@ def embed_position(pos, docstring):
return
doc
def
analyse_type_annotation
(
annotation
,
env
):
def
analyse_type_annotation
(
annotation
,
env
,
assigned_value
=
None
):
base_type
=
None
is_ambiguous
=
False
explicit_pytype
=
explicit_ctype
=
False
if
annotation
.
is_dict_literal
:
warning
(
annotation
.
pos
,
...
...
@@ -88,6 +89,13 @@ def analyse_type_annotation(annotation, env):
warning
(
annotation
.
pos
,
"Duplicate type declarations found in signature annotation"
)
arg_type
=
annotation
.
analyse_as_type
(
env
)
if
annotation
.
is_name
and
not
annotation
.
cython_attribute
and
annotation
.
name
in
(
'int'
,
'long'
,
'float'
):
# Map builtin numeric Python types to C types in safe cases.
if
assigned_value
is
not
None
and
arg_type
is
not
None
and
not
arg_type
.
is_pyobject
:
assigned_type
=
assigned_value
.
infer_type
(
env
)
if
assigned_type
and
assigned_type
.
is_pyobject
:
# C type seems unsafe, e.g. due to 'None' default value => ignore annotation type
is_ambiguous
=
True
arg_type
=
None
# ignore 'int' and require 'cython.int' to avoid unsafe integer declarations
if
arg_type
in
(
PyrexTypes
.
c_long_type
,
PyrexTypes
.
c_int_type
,
PyrexTypes
.
c_float_type
):
arg_type
=
PyrexTypes
.
c_double_type
if
annotation
.
name
==
'float'
else
py_object_type
...
...
@@ -100,6 +108,8 @@ def analyse_type_annotation(annotation, env):
"Python type declaration in signature annotation does not refer to a Python type"
)
base_type
=
CAnalysedBaseTypeNode
(
annotation
.
pos
,
type
=
arg_type
,
is_arg
=
True
)
elif
is_ambiguous
:
warning
(
annotation
.
pos
,
"Ambiguous types in annotation, ignoring"
)
else
:
warning
(
annotation
.
pos
,
"Unknown type declaration in annotation, ignoring"
)
return
base_type
,
arg_type
...
...
@@ -890,7 +900,7 @@ class CArgDeclNode(Node):
annotation
=
self
.
annotation
if
not
annotation
:
return
None
base_type
,
arg_type
=
analyse_type_annotation
(
annotation
,
env
)
base_type
,
arg_type
=
analyse_type_annotation
(
annotation
,
env
,
assigned_value
=
self
.
default
)
if
base_type
is
not
None
:
self
.
base_type
=
base_type
return
arg_type
...
...
tests/run/annotation_typing.pyx
View file @
dfd73ac6
...
...
@@ -215,6 +215,22 @@ class LateClass(object):
pass
def
py_float_default
(
price
:
float
=
None
,
ndigits
=
4
):
"""
Python default arguments should prevent C type inference.
>>> py_float_default()
(None, 4)
>>> py_float_default(2)
(2, 4)
>>> py_float_default(2.0)
(2.0, 4)
>>> py_float_default(2, 3)
(2, 3)
"""
return
price
,
ndigits
_WARNINGS
=
"""
8:32: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly.
8:47: Dicts should no longer be used as type annotations. Use 'cython.int' etc. directly.
...
...
@@ -223,6 +239,7 @@ _WARNINGS = """
8:85: Python type declaration in signature annotation does not refer to a Python type
8:85: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly.
211:44: Unknown type declaration in annotation, ignoring
218:29: Ambiguous types in annotation, ignoring
# BUG:
46:6: 'pytypes_cpdef' redeclared
121:0: 'struct_io' redeclared
...
...
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