-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[mod] switching from pyright to basedpyright (plus first rules) #5075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| categories: list[str] | ||
| disabled: bool | ||
| display_error_messages: bool | ||
| enable_http: bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also possible to override values from the outgoing: yaml settings as mentioned in
searxng/docs/admin/settings/settings_engines.rst
Lines 59 to 74 in 4942c9b
| # overwrite values from section 'outgoing:' | |
| enable_http2: false | |
| retries: 1 | |
| max_connections: 100 | |
| max_keepalive_connections: 10 | |
| keepalive_expiry: 5.0 | |
| using_tor_proxy: false | |
| proxies: | |
| http: | |
| - http://proxy1:8080 | |
| - http://proxy2:8080 | |
| https: | |
| - http://proxy1:8080 | |
| - http://proxy2:8080 | |
| - socks5://user:password@proxy3:1080 | |
| - socks5h://user:password@proxy4:1080 |
enable_http2. I'm not sure if it makes sense to add them here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it makes sense to add them here?
🤔 are these options really related to the engine .. for me these options are related to the outgoing: settings, since it overwrites those settings .. before we cover these types in the engine config, we should first port the type definitions in settings_defaults.py to msgspec.Struct.
When we have a msgspec.Struct for ...
searxng/searx/settings_defaults.py
Lines 216 to 235 in 5e7109c
| 'outgoing': { | |
| 'useragent_suffix': SettingsValue(str, ''), | |
| 'request_timeout': SettingsValue(numbers.Real, 3.0), | |
| 'enable_http2': SettingsValue(bool, True), | |
| 'verify': SettingsValue((bool, str), True), | |
| 'max_request_timeout': SettingsValue((None, numbers.Real), None), | |
| 'pool_connections': SettingsValue(int, 100), | |
| 'pool_maxsize': SettingsValue(int, 10), | |
| 'keepalive_expiry': SettingsValue(numbers.Real, 5.0), | |
| # default maximum redirect | |
| # from https://github.com/psf/requests/blob/8c211a96cdbe9fe320d63d9e1ae15c5c07e179f8/requests/models.py#L55 | |
| 'max_redirects': SettingsValue(int, 30), | |
| 'retries': SettingsValue(int, 0), | |
| 'proxies': SettingsValue((None, str, dict), None), | |
| 'source_ips': SettingsValue((None, str, list), None), | |
| # Tor configuration | |
| 'using_tor_proxy': SettingsValue(bool, False), | |
| 'extra_proxy_timeout': SettingsValue(int, 0), | |
| 'networks': {}, | |
| }, |
we can rethink how we add this type to the engine builtins .. but I hope we will implement a Engine class before we add more and more typing for all these monkey patched stuff into the builtins.
BTW we should not mix all these names of config options into a flat namespace / on the long term we should replace it by outgoing section in the engine config. This will simplify the coding a lot and prevents name collisions.
- name: example
engine: example
shortcut: demo
outgoing:
enable_http2: false
proxies:
http:
- http://proxy1:8080
- http://proxy2:8080
https:
- socks5://user:password@proxy3:1080
- socks5h://user:password@proxy4:1080There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW we should not mix all these names of config options into a flat namespace / on the long term we should replace it by outgoing section in the engine config. This will simplify the coding a lot and prevents name collisions.
Totally agree on that, I found it very confusing when I used it once to change enable_http2.
Have we considered Ruff? I came across this now while looking into uv, at first glance, looks like it replaces every existing linters and formatters, like Biome in JS stack does. |
I had a look at ruff in some other projects I had / hatch for example uses ruff by default. However, I was not convinced by the quality... compared to the trio consisting of pylint, basedpyright & black. I'm aware that many people prefer Ruff because it's generally considered the super-fast, all-purpose tool. Personally, I prefer basedpyright's LSP (I haven't encountered any speed issues yet), it's easy to integrate into the IDE, and (as far as I know) has more options. I'd also like to keep pylint in our toolchain, as I've had good experiences with it as well. Please keep in mind that we only have a very small amount of static types. The vast majority of the code is and probably always will be dynamically typed. For formatting, we have a third tool (black). I'm aware that it certainly seems easier to have one tool for all three tasks (the all-purpose tool). But true to the Linux principle of "do one thing and do it well," I think it's justified to have three tools in use here. |
| timeout: int | ||
| tokens: list[str] | ||
| using_tor_proxy: bool | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EngineTraits type at module name traits is missed ..
searxng/searx/engines/__init__.py
Lines 138 to 139 in 5e7109c
| trait_map = EngineTraitsMap.from_data() | |
| trait_map.set_traits(engine) |
searxng/searx/enginelib/traits.py
Line 192 in 5e7109c
| engine.traits = traits |
While implementing #5101 I looked once more into Ruff & LSP .. they had an rewrite of the ruff-lsp last year: With the new TL;DR; basedpyright is a Static Type Checker for Python and the LSP implements (all) function one would expect, here by example we have a Literal with a catalog of names ( A literal not from the catalog is detected as a type issue .. The LSP from Ruff is not able to follow type hints .. no type issue is reported ( All issues Ruff LSP has found are trivials, like unknown names (e.g. The announcement of ASTRAL last year was still... .. but they are still far from a minimal feature set. ASTRAL repeatedly emphasizes (with a pinch of arrogance) how fast their tools are... which should surely be a given if these tools just implement a fraction of the features. "Faster" cannot be equated with "more accurate." |
I recommend to use basedpyright (not pyright) and I install all the needed tools into my HOME by pipx: Don't be confused by the PyLSP names:
|
From what's listed in |
|
FWIW I also tested the language server from https://zubanls.com/ (alpha) .. When it comes to speed, the emphasis is also on “speed” (I'm tired of hearing it) ... Zuban compares itself to mypy in terms of speed ... but unfortunately, it doesn't come close to having the same range of functions. I feel like I've tried every Python LSP on the planet :-) ... Most are reimplementations of linterns, but they can't be compared to pylint either. Others integrate pylint, but still don't have strong type checking. In the IDE, pyright and its successor basedpyright work best. They show the programmer most of the problems and offer good support for autocompletion. In the build chain, pylint provides us with additional checks that a static type checker does not have in its scope, and we have black for code formatting... I think we are in a very good position and should not change our Python toolchain any further. If there are no objections, I would like to merge the PR. // There will still be a lot of fine-tuning possible in the configuration in |
|
A little OT, but for the sake of completeness, here is an excerpt from my scrapbook of what I have installed for my SearXNG development tasks: |
|
Shouldn't the root package.json be removed, as it was only used for pyright? |
|
FYI: last force push was a just a rebase on branch master to solve a conflict. |
pyrightconfig.json : for the paths searx, searxng_extra and tests, individual rules were defined (for example, in test fewer / different rules are needed than in the searx package searx/engines/__builtins__.pyi : The builtin types that are added to the global namespace of a module by the intended monkey patching of the engine modules / replaces the previous filtering of the stdout using grep. test.pyright_modified (utils/lib_sxng_test.sh) : static type check of local modified files not yet commited make test : prerequisite 'test.pyright' has been replaced by 'test.pyright_modified' searx/engines/__init__.py, searx/enginelib/__init__.py : First, minimal typifications that were considered necessary.
I removed the pyright dependency diff but not the entire file: The |
I tried various type checkers, focusing primarily on the features of the LSP.
basedyright, or rather, the LSPbasedpyright-langserver, convinced me.For integration into your IDE (editor) use the basedpyright-langserver (LSP) installed by
pipx basedpyrightand read:The
$REPO_ROOT/pyrightconfig.jsonuses the virtualenv found in$REPO_ROOT/local/py3and can be created by amake pyenvpyrightconfig.json:for the paths searx, searxng_extra and tests, individual rules were defined (for example, in test fewer / different rules are needed than in the searx package
searx/engines/__builtins__.pyi:The builtin types that are added to the global namespace of a module by the intended monkey patching of the engine modules / replaces the previous filtering of the stdout using grep.
test.pyright_modified (utils/lib_sxng_test.sh):static type check of local modified files not yet commited
make test:prerequisite
test.pyrighthas been replaced bytest.pyright_modifiedsearx/engines/__init__.py,searx/enginelib/__init__.py:First, minimal typifications that were considered necessary.
I think we need to put more effort into typing, but we shouldn't try to type everything either; that just creates unnecessary work and is frustrating. Python is and remains a dynamically typed language, but types can improve quality and, with the LSP (IDE), positively impact productivity. In this sense, we should also align our goals with regard to typing.