Skip to content

Instantly share code, notes, and snippets.

@dimaqq
dimaqq / test_main_type_hint.py
Last active August 30, 2024 06:30
Type tests using pyright
from dataclasses import dataclass
from typing import Optional, Protocol, Type
import ops
class CallableWithCharmClassOnly(Protocol):
"""Encapsulate main function type for simple charms.
@dimaqq
dimaqq / test_main_invocations.py
Last active August 30, 2024 06:31
Type tests via custom type guard
import ... # exinsting code
@pytest_fixture
def charm_env(monkeypatch, ...): # existing code
...
from typing import Type, TypeVar, Any, Optional, Callable, Mapping
from typing_extensions import TypeGuard
import inspect
@dimaqq
dimaqq / ops __init__.py
Last active August 28, 2024 01:17
Fixing ops.main() type hints, the protocol version
from typing import Protocol, cast
import ._main
import .main as _legacy_main
class Main(Protocol):
def __call__(self, arg1: Arg1, ...): ...
def main(self, arg1: Arg1, ...): ...
main = cast(Main, _main.main)
main.main = _legacy_main.main
@dimaqq
dimaqq / ops __init__.py
Last active August 28, 2024 01:09
Fixing ops.main() type hints, the callable class version
import ._main
import .main as _legacy_main
class Main:
__call__ = staticmethod(_main.main)
main = staticmethod(_legacy_main.main)
main = Main()
@dimaqq
dimaqq / ops __init__.py
Last active August 28, 2024 01:07
Fixing ops.main() type hints, the callable module version
import .main as _main
main = _main._CallableMainModule('ops.main', _main.__doc__)

Totally OT: if you wanna test nogil out:

  • macos 3.13b2 installer includes the nogil build, needs to be exp[licitly selected
    • at "Installation type" click "Customise" instead of "Install"
  • ubuntu (and similar), the deadsnakes project apparently ships nogil versions too
    • apt/nala install python3.13-nogil
  • GitHub actions, via deadsnakes/action@v3.1.0 with nogil:true
{
"Profiles": [
{
"Ansi 7 Color (Light)" : {
"Red Component" : 0.93333333333333335,
"Color Space" : "sRGB",
"Blue Component" : 0.83529411764705885,
"Green Component" : 0.90980392156862744
},
"Ansi 15 Color (Light)" : {
" -*- coding: utf-8 -*-
" scriptencoding utf-8
set termencoding=utf-8
set termguicolors
set encoding=utf-8
setglobal fileencoding=utf-8
syntax on
set hls
set expandtab
set colorcolumn=88
alias v=vim
alias fd=fdfind
alias v=vim
#alias top=htop
alias gc="git commit"
alias gcam="git commit -am"
" -*- coding: utf-8 -*-
" scriptencoding utf-8
set termencoding=utf-8
set termguicolors
set encoding=utf-8
setglobal fileencoding=utf-8
syntax on
set hls
set expandtab
set colorcolumn=88