Python 3 type hints in Python 2










0















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?










share|improve this question



















  • 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















0















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?










share|improve this question



















  • 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













0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer

























  • 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











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
);



);













draft saved

draft discarded


















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









0














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.






share|improve this answer

























  • 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
















0














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.






share|improve this answer

























  • 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














0












0








0







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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




















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands