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 from typing module ignored

See original GitHub issue
  • Problem or Bug:

    “imports” conditionally added in modules with TYPE_CHECKING are really imported which shouldn’t be.

from __future__ import annotations
from typing import Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from ....common.devices.base.device import DEVICE_TYPES  # this should only be imported by IDEs for type hinting, not at runtime

from ..devices.controller import Controller
from .signal import Signal

class Api(object):

    def __init__(self):
        self.app = None
        self.mainwindow = None
        self.controller: Optional[Controller] = None
        self.device: Optional[DEVICE_TYPES] = None
        self.newDevice: Optional[Signal] = None
  • Nuitka version, full Python version and Platform (Windows, OSX, Linux …)

    wine python3 -m nuitka --version 0.6.17.2 Commercial: None Python: 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] Executable: C:\Program Files\Python37\python3.exe OS: Windows Arch: x86_64

  • How did you install Nuitka and Python

    with pip3

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
kayhayencommented, Oct 21, 2021

Yes, I think the import after the TYPE_CHECKING import currently makes all module variables considered “unknown”, i.e. could be deleted, or any other value, such than the if statement then ignores it.

On my agenda is to excempt the so called hard imports and potentially other kinds of values from this level of paranoia, but it will take a bit of changes. Above code currently optimized into:

TYPE_CHECKING = False
from .setiInterfaceParams import SetiInterfaceParams as InterfaceItem
if TYPE_CHECKING:

Obviously, that looses the information of origin, and therefore module variables need to be split into two kinds, one which is more trustworthy than the other, and then of course, to allow users to accept that all module variables of a given module are considered to not have outside writes.

So therefore this will have to wait until I figure out the refactorings necessary. btw, the same issue is affecting sys.version_info and importlib.import_module and many other important things, so it’s certainly going to come, but it’s also not easy. If you rearrage above code though, moving the if statement up, it would work already now. Naturally not a solution that is practical.

0reactions
kayhayencommented, Dec 3, 2021

This is part of the 0.6.18 release. On factory, the function level usage is implemented and will be in 0.6.19, use that if you encounter problems with this not being optimized yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common issues and solutions - mypy 0.991 documentation
You can use a # type: ignore comment to silence the type checker on a particular line. For example, let's say our code...
Read more >
How can mypy ignore a single line in a source file?
I'm using mypy in my python project for type checking. I'm also using PyYAML for reading and ...
Read more >
ignore to skip type checking in a file · Issue #626 · python/mypy
As per PEP 484 discussion, it should be possible to use # type: ignore to skip type checking in the rest of a...
Read more >
Python Type Checking (Guide) - Real Python
Code without type hints will be ignored by the static type checker. Therefore, you can start adding types to critical components, and continue...
Read more >
Python Type Hints - How to Manage “type: ignore” Comments ...
It seems inevitable that large projects need some # type: ignore comments, to work around type checking in tricky cases.
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