![]() |
#1 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 137
Karma: 642206
Join Date: Mar 2021
Device: Kindle Voyage
|
Avoiding "is not defined" errors with Pylance and VSCode
Does anyone use Microsoft's Pylance static type checker in VSCode and know how to disable this type of error.
For example, given the following snippet Code:
# noinspection PyUnreachableCode if False : load_translations = _ = ngettext = lambda x=None, y=None, z=None: x load_translations() "load_translations" is not defined Pylance (reportUndefinedVariable) I know I have three options 1) Disable pylance 2) Disable this type of error across the whole project 3) Pollute the codebase by adding "# type: ignore" to hundreds of lines. But I would prefer not to have do any of these and was wondering if there was a better way? |
![]() |
![]() |
![]() |
#2 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,398
Karma: 27756918
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Why do you need to do that at all? Just import those functions explicitly, like this
from calibre.utils.localization import gettext as _, ngettext, load_translations |
![]() |
![]() |
Advert | |
|
![]() |
#3 | |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 137
Karma: 642206
Join Date: Mar 2021
Device: Kindle Voyage
|
Quote:
(Although I have been a programmer for several decades, I would still consider myself a beginner as far as python is concerned and they are many things that I don't fully understand.) In particular, looking at the git history, this code used to call the load_translations and _ methods without actually importing them and I have no idea how or where it finds these methods and hence what they did. Later it was amended and there is now a section of code which does Code:
try: from calibre.utils.localization import _ as _c from calibre.utils.localization import ngettext as ngettext_c except ImportError: # fallback to global _ for calibre<6.12 _c = _ ngettext_c = ngettext It does not import load_translations from calibre.utils.localization and I would presume that means it is still loading another method from somewhere. Also, the fact that it renames calibre's _ method as _c can only mean that there was a deliberate decision to use the original _ method in some places and the calibre version in others and I have no idea why. |
|
![]() |
![]() |
![]() |
#4 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,398
Karma: 27756918
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Those are all functions that were defined as globals in calibre a long time ago. _ and gettext and ngettext can now be imported perfectly safely. If you are worried about doing so for compat reasons you can do the following:
Code:
from typing import TYPE_CHECKING if TYPE_CHECKING: from calibre.utils.localization import ... else: the original code |
![]() |
![]() |
![]() |
Tags |
typing, vscode |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Cloud Storage : Errors :"cannot fetch list" and "Could not save file" | Villard | KOReader | 5 | 05-25-2025 02:50 AM |
display the self-defined "read" icon | notebook | Server | 6 | 09-08-2022 10:11 AM |
"fragment identifier is not defined" question | eBooked | Workshop | 4 | 07-22-2022 05:07 PM |
"Entity 'nbsp' not defined" error | mrprobert | Sigil | 11 | 03-19-2019 09:01 AM |
question about "user defined usb device" plugin | Kaverin | Plugins | 2 | 03-08-2014 09:59 PM |