Skip to content

Instantly share code, notes, and snippets.

@zekefarwell
Created May 1, 2020 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekefarwell/f4d25de62e0ff8f9d8bf3becab1a948d to your computer and use it in GitHub Desktop.
Save zekefarwell/f4d25de62e0ff8f9d8bf3becab1a948d to your computer and use it in GitHub Desktop.
Python Import Test Cases
from __future__ import annotations
import thing as t
class Logger:
# This type hint would throw a circular error because
# without the __future__ import above.
# That import defers the execution of the typehint until after
# thing is fully initialized
def log(self, two: t.Two):
print(f'{two.name}: {two.get_info()}')
# No circular error because full module imported
# instead of a class or function within
import thing
# This would be an error because we attempt to
# access One before the thing module is fully initialized
# from thing import One
class Printer:
def print(self):
# thing module is fully initialized by the time we use
# the One class within thing so no issue here
one = thing.One()
print(f'{one.name}: {one.get_info()}')
import thing
thing_one = thing.One()
thing_one.print()
thing_two = thing.Two()
thing_two.log()
import logger
import printer
class One:
name = "Thing One"
def print(self):
printer.Printer().print()
def get_info(self):
return "The first thing"
class Two:
name = "Thing Two"
def log(self):
logger.Logger().log(self)
def get_info(self):
return "The second thing"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment