Python 3 type hints in Python 2
I have python def definition which seems working for python3:
def get_default_device(use_gpu: bool = True) -> cl.Device:
Under python2 I get the following syntax error:
root:~/pyopencla/ch3# python map_copy.py
Traceback (most recent call last):
File "map_copy.py", line 9, in <module>
import utility
File "/home/root/pyopencla/ch3/utility.py", line 6
def get_default_device(use_gpu: bool = True) -> cl.Device:
^
SyntaxError: invalid syntax
How to make type hints compatible with python2?
python python-3.x python-2.x python-typing
add a comment |
I have python def definition which seems working for python3:
def get_default_device(use_gpu: bool = True) -> cl.Device:
Under python2 I get the following syntax error:
root:~/pyopencla/ch3# python map_copy.py
Traceback (most recent call last):
File "map_copy.py", line 9, in <module>
import utility
File "/home/root/pyopencla/ch3/utility.py", line 6
def get_default_device(use_gpu: bool = True) -> cl.Device:
^
SyntaxError: invalid syntax
How to make type hints compatible with python2?
python python-3.x python-2.x python-typing
3
By removing the type hints
– Brad Solomon
Nov 14 '18 at 18:15
maybe mypy.readthedocs.io/en/latest/…
– eyllanesc
Nov 14 '18 at 18:18
add a comment |
I have python def definition which seems working for python3:
def get_default_device(use_gpu: bool = True) -> cl.Device:
Under python2 I get the following syntax error:
root:~/pyopencla/ch3# python map_copy.py
Traceback (most recent call last):
File "map_copy.py", line 9, in <module>
import utility
File "/home/root/pyopencla/ch3/utility.py", line 6
def get_default_device(use_gpu: bool = True) -> cl.Device:
^
SyntaxError: invalid syntax
How to make type hints compatible with python2?
python python-3.x python-2.x python-typing
I have python def definition which seems working for python3:
def get_default_device(use_gpu: bool = True) -> cl.Device:
Under python2 I get the following syntax error:
root:~/pyopencla/ch3# python map_copy.py
Traceback (most recent call last):
File "map_copy.py", line 9, in <module>
import utility
File "/home/root/pyopencla/ch3/utility.py", line 6
def get_default_device(use_gpu: bool = True) -> cl.Device:
^
SyntaxError: invalid syntax
How to make type hints compatible with python2?
python python-3.x python-2.x python-typing
python python-3.x python-2.x python-typing
edited Nov 14 '18 at 18:33
Mad Physicist
37.7k1674106
37.7k1674106
asked Nov 14 '18 at 18:15
user3428154user3428154
809
809
3
By removing the type hints
– Brad Solomon
Nov 14 '18 at 18:15
maybe mypy.readthedocs.io/en/latest/…
– eyllanesc
Nov 14 '18 at 18:18
add a comment |
3
By removing the type hints
– Brad Solomon
Nov 14 '18 at 18:15
maybe mypy.readthedocs.io/en/latest/…
– eyllanesc
Nov 14 '18 at 18:18
3
3
By removing the type hints
– Brad Solomon
Nov 14 '18 at 18:15
By removing the type hints
– Brad Solomon
Nov 14 '18 at 18:15
maybe mypy.readthedocs.io/en/latest/…
– eyllanesc
Nov 14 '18 at 18:18
maybe mypy.readthedocs.io/en/latest/…
– eyllanesc
Nov 14 '18 at 18:18
add a comment |
1 Answer
1
active
oldest
votes
Function annotations were introduced in PEP 3107 for Python 3.0. The usage of annotations as type hints was formalized in in PEP 484 for Python 3.5+.
Any version before 3.0 then will not support the syntax you are using for type hints at all. However, PEP 484 offers a workaround, which some editors may choose to honor. In your case, the hints would look like this:
def get_default_device(use_gpu=True):
# type: (bool) -> cl.Device
...
or more verbosely,
def get_default_device(use_gpu=True # type: bool
):
# type: (...) -> cl.Device
...
The PEP explicitly states that this form of type hinting should work for any version of Python, if it is supported at all.
PEP484 isn't the relevant source in this case. Type hints are just an application of function annotations, which where introduced by PEP3107 for Python 3.0.
– chepner
Nov 14 '18 at 18:34
@chepner. I've updated. While I agree that annotations were valid since Py 3.0, the relevant PEP is still 484 (which references 3107).
– Mad Physicist
Nov 14 '18 at 18:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306458%2fpython-3-type-hints-in-python-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Function annotations were introduced in PEP 3107 for Python 3.0. The usage of annotations as type hints was formalized in in PEP 484 for Python 3.5+.
Any version before 3.0 then will not support the syntax you are using for type hints at all. However, PEP 484 offers a workaround, which some editors may choose to honor. In your case, the hints would look like this:
def get_default_device(use_gpu=True):
# type: (bool) -> cl.Device
...
or more verbosely,
def get_default_device(use_gpu=True # type: bool
):
# type: (...) -> cl.Device
...
The PEP explicitly states that this form of type hinting should work for any version of Python, if it is supported at all.
PEP484 isn't the relevant source in this case. Type hints are just an application of function annotations, which where introduced by PEP3107 for Python 3.0.
– chepner
Nov 14 '18 at 18:34
@chepner. I've updated. While I agree that annotations were valid since Py 3.0, the relevant PEP is still 484 (which references 3107).
– Mad Physicist
Nov 14 '18 at 18:39
add a comment |
Function annotations were introduced in PEP 3107 for Python 3.0. The usage of annotations as type hints was formalized in in PEP 484 for Python 3.5+.
Any version before 3.0 then will not support the syntax you are using for type hints at all. However, PEP 484 offers a workaround, which some editors may choose to honor. In your case, the hints would look like this:
def get_default_device(use_gpu=True):
# type: (bool) -> cl.Device
...
or more verbosely,
def get_default_device(use_gpu=True # type: bool
):
# type: (...) -> cl.Device
...
The PEP explicitly states that this form of type hinting should work for any version of Python, if it is supported at all.
PEP484 isn't the relevant source in this case. Type hints are just an application of function annotations, which where introduced by PEP3107 for Python 3.0.
– chepner
Nov 14 '18 at 18:34
@chepner. I've updated. While I agree that annotations were valid since Py 3.0, the relevant PEP is still 484 (which references 3107).
– Mad Physicist
Nov 14 '18 at 18:39
add a comment |
Function annotations were introduced in PEP 3107 for Python 3.0. The usage of annotations as type hints was formalized in in PEP 484 for Python 3.5+.
Any version before 3.0 then will not support the syntax you are using for type hints at all. However, PEP 484 offers a workaround, which some editors may choose to honor. In your case, the hints would look like this:
def get_default_device(use_gpu=True):
# type: (bool) -> cl.Device
...
or more verbosely,
def get_default_device(use_gpu=True # type: bool
):
# type: (...) -> cl.Device
...
The PEP explicitly states that this form of type hinting should work for any version of Python, if it is supported at all.
Function annotations were introduced in PEP 3107 for Python 3.0. The usage of annotations as type hints was formalized in in PEP 484 for Python 3.5+.
Any version before 3.0 then will not support the syntax you are using for type hints at all. However, PEP 484 offers a workaround, which some editors may choose to honor. In your case, the hints would look like this:
def get_default_device(use_gpu=True):
# type: (bool) -> cl.Device
...
or more verbosely,
def get_default_device(use_gpu=True # type: bool
):
# type: (...) -> cl.Device
...
The PEP explicitly states that this form of type hinting should work for any version of Python, if it is supported at all.
edited Nov 14 '18 at 18:34
answered Nov 14 '18 at 18:30
Mad PhysicistMad Physicist
37.7k1674106
37.7k1674106
PEP484 isn't the relevant source in this case. Type hints are just an application of function annotations, which where introduced by PEP3107 for Python 3.0.
– chepner
Nov 14 '18 at 18:34
@chepner. I've updated. While I agree that annotations were valid since Py 3.0, the relevant PEP is still 484 (which references 3107).
– Mad Physicist
Nov 14 '18 at 18:39
add a comment |
PEP484 isn't the relevant source in this case. Type hints are just an application of function annotations, which where introduced by PEP3107 for Python 3.0.
– chepner
Nov 14 '18 at 18:34
@chepner. I've updated. While I agree that annotations were valid since Py 3.0, the relevant PEP is still 484 (which references 3107).
– Mad Physicist
Nov 14 '18 at 18:39
PEP484 isn't the relevant source in this case. Type hints are just an application of function annotations, which where introduced by PEP3107 for Python 3.0.
– chepner
Nov 14 '18 at 18:34
PEP484 isn't the relevant source in this case. Type hints are just an application of function annotations, which where introduced by PEP3107 for Python 3.0.
– chepner
Nov 14 '18 at 18:34
@chepner. I've updated. While I agree that annotations were valid since Py 3.0, the relevant PEP is still 484 (which references 3107).
– Mad Physicist
Nov 14 '18 at 18:39
@chepner. I've updated. While I agree that annotations were valid since Py 3.0, the relevant PEP is still 484 (which references 3107).
– Mad Physicist
Nov 14 '18 at 18:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306458%2fpython-3-type-hints-in-python-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
By removing the type hints
– Brad Solomon
Nov 14 '18 at 18:15
maybe mypy.readthedocs.io/en/latest/…
– eyllanesc
Nov 14 '18 at 18:18