DEV Community

Mario Hernandez
Mario Hernandez

Posted on

Python Typing, between 3.6 and 3.7

I recently discovered that Python has Typing for classes and functions, so yeeeiii

but I have a little problem with backwards compatibility

from .utils import StdReturn

def simpleFunc(data: str) -> StdReturn:
  """ My Awesome Function """
  ret = StdReturn()
  return ret

This example works fine in Python 3.7 and 3.8 (with an extra import)

And the Python 3.6 way is like this (with no extra imports)

from .utils import StdReturn

def simpleFunc(data: str) -> "StdReturn":
  """ My Awesome Function """
  ret = StdReturn()
  return ret

I'm OK with using one way or the other, but if I want to have Support from 3.6 and up, how should i do it? All my searches take me to configure virtualenv and install different versions of python in the system and not fragmentation within my python package.

I'm leaning to use files for 3.6 and another source for 3.7, but it's very hard to maintain different versions within the code. Do you know if there is a python way to do this?

PS: The example code is not copy paste, they are just as an example

Discussion (2)

Collapse
csgeek profile image
csgeek

I think the typing in Python is called hints, I may be wrong.

One issue I have with it though, even though you give it a hint it doesn't actually enforce it. unless that changes I find the hints or typing fairly pointless.

Collapse
quintisimo profile image
Quintus Cardozo

The types are not evaluated at runtime. You can use pyright in vscode to get type errors in your editor.