question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

✨Type checking imports for all type hints

See original GitHub issue

Task

Only import type hints when TYPE_CHECKING is true.

Must be present:

from __future__ import annotations

from typing import TYPE_CHECKING

Example

from __future__ import annotations

from typing import TYPE_CHECKING
from dataclasses import dataclass


if TYPE_CHECKING:
    from typing import List


@dataclass
class Foo:
    bar: List[int] = None

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
ooliver1commented, Oct 6, 2021

Cancel that I found an option to fix that, says “expensive” but I guess it’s better than moving all imports

1reaction
ooliver1commented, Oct 12, 2021

as seen from 769cefa7542ff6f1ddac3ddb890cb8f18fb41c02 it is in my doc branch and will be PR’d along with docs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python type hinting without cyclic imports - Stack Overflow
For people struggling with cyclic imports when importing class only for Type checking: you will likely want to use a Forward ...
Read more >
Python Type Checking (Guide) - Real Python
In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit...
Read more >
Type hints cheat sheet - mypy 0.991 documentation
Type hints cheat sheet#. This document is a quick cheat sheet showing how to use type annotations for various common types in Python....
Read more >
Python Types Intro - FastAPI
FastAPI is all based on these type hints, they give it many advantages and benefits. ... As the type, put the List that...
Read more >
Python Type Hints - How to Fix Circular Imports - Adam Johnson
The answer is to use the special typing.TYPE_CHECKING constant. This is hardcoded to False , but set to True by type checkers like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found