Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old Yesterday, 12:54 PM   #1
sgmoore
Zealot
sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.
 
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()
The first few lines are enough to stop ruff and mypy from complaining about the method call but not Pylance which displays an error like

"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?
sgmoore is offline   Reply With Quote
Old Yesterday, 09:59 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
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
kovidgoyal is online now   Reply With Quote
Old Today, 04:41 AM   #3
sgmoore
Zealot
sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.sgmoore ought to be getting tired of karma fortunes by now.
 
Posts: 137
Karma: 642206
Join Date: Mar 2021
Device: Kindle Voyage
Quote:
Originally Posted by kovidgoyal View Post
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
I guess the main reason is that I am also reluctant to change code that I don't fully understand (and it's probably obvious that I'm not the original author of the code).

(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
This I mostly understand, but note :

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.
sgmoore is offline   Reply With Quote
Old Today, 09:29 AM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
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
This should work with all type checkers. As for load_translations() that's specific to calibre plugins and is documented in the manual https://manual.calibre-ebook.com/creating_plugins.html
kovidgoyal is online now   Reply With Quote
Reply

Tags
typing, vscode


Forum Jump

Similar Threads
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


All times are GMT -4. The time now is 09:44 AM.


MobileRead.com is a privately owned, operated and funded community.