Should you import all classes you use in Python?

coding-style, python

Solution

Yes, it's completely ok. `some_class_instance` might be anything, it doesn't have to be an instance of `SomeClass`. You might want to pass an instance that looks just like `SomeClass`, but uses a different implementation for testing purposes, for example.

Problem

Python's lack of static typing makes it possible to use and rely on classes without importing them. Should you import them anyway? Does it matter? Example someclass.py ``` class SomeClass: def __init__(self, some_value): self.some_value = some_value ``` someclient.py ``` class SomeClient: def __init__(self, some_class_instance): self.some_class_helper = some_class_instance ``` Here, the functionality of `SomeClient` clearly relies on `SomeClass` or at least something that behaves like it. However, someclient.py will work just fine without `import someclass`. Is this ok? It feels wrong to use something without saying anywhere that you're even using it.

Original source