canonical / canonical/cos-proxy-operator

Fix typing

Open
#165 0 comments 0 reactions 0 assignees View on GitHub
good first issue Status: Triage Type: Enhancement
Dominant language
Python
Stars
3
Forks
12
PR merge metrics
No merged PRs in 30d

Description

### Enhancement Proposal

This issue was found while working in this PR: #163
It was already opened in https://github.com/microsoft/pyright/issues/9539

They say this is unlikely to be a bug in `pyright`

However, until version 1.1.381 everything was OK:

```shell
✗ tox -re static-charm
static-charm: remove tox env folder /home/jose/trabajos/canonical/repos/cos-proxy-operator/.tox/static-charm
static-charm: install_deps> python -I -m pip install httpcore==0.14.7 pyright==1.1.381 responses==0.20.0 -r /home/jose/trabajos/canonical/repos/cos-proxy-operator/requirements.txt
static-charm: commands[0]> pyright /home/jose/trabajos/canonical/repos/cos-proxy-operator/src
WARNING: there is a new pyright version available (v1.1.381 -> v1.1.389).
Please install the new version or set PYRIGHT_PYTHON_FORCE_VERSION to `latest`

0 errors, 0 warnings, 0 informations
static-charm: OK (60.07=setup[43.68]+cmd[16.39] seconds)
congratulations :) (60.61 seconds)
```

But with `1.1.382`+ I see these errors:

```shell
✗ tox -e static-charm
static-charm: commands[0]> pyright /home/jose/trabajos/canonical/repos/cos-proxy-operator/src
WARNING: there is a new pyright version available (v1.1.382 -> v1.1.389).
Please install the new version or set PYRIGHT_PYTHON_FORCE_VERSION to `latest`

/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py
/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py:309:37 - error: Argument missing for parameter "static_configs" (reportCallIssue)
/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py:309:54 - error: Argument expression after ** must be a mapping with a "str" key type (reportCallIssue)
/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py:330:38 - error: Arguments missing for parameters "name", "rules" (reportCallIssue)
/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py:330:56 - error: Argument expression after ** must be a mapping with a "str" key type (reportCallIssue)
/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py:639:64 - error: Argument of type "str | list[Unknown] | dict[Any, Any] | Unknown | Any" cannot be assigned to parameter "job_name" of type "str" in function "remove_prometheus_jobs"
  Type "str | list[Unknown] | dict[Any, Any] | Unknown | Any" is not assignable to type "str"
    "dict[Any, Any]" is not assignable to "str" (reportArgumentType)
/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py:664:35 - error: No overloads for "__getitem__" match the provided arguments (reportCallIssue)
/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py:664:35 - error: Argument of type "Literal['labels']" cannot be assigned to parameter "s" of type "slice" in function "__getitem__"
  "Literal['labels']" is not assignable to "slice" (reportArgumentType)
/home/jose/trabajos/canonical/repos/cos-proxy-operator/src/charm.py:665:17 - error: Argument of type "Dict[str, Any] | list[Unknown] | dict[Any, Any] | Unknown | Any" cannot be assigned to parameter "unit_rules" of type "dict[Unknown, Unknown]" in function "set_alert_rule_data"
  Type "Dict[str, Any] | list[Unknown] | dict[Any, Any] | Unknown | Any" is not assignable to type "dict[Unknown, Unknown]"
    "list[Unknown]" is not assignable to "dict[Unknown, Unknown]" (reportArgumentType)
8 errors, 0 warnings, 0 informations
static-charm: exit 1 (15.58 seconds) /home/jose/trabajos/canonical/repos/cos-proxy-operator> pyright /home/jose/trabajos/canonical/repos/cos-proxy-operator/src pid=719891
static-charm: FAIL code 1 (15.73=setup[0.15]+cmd[15.58] seconds)
evaluation failed :( (16.31 seconds)
```

## Context

For the first 2 errors ([line 309](https://github.com/canonical/cos-proxy-operator/blob/main/src/charm.py#L309))

![Image](https://github.com/user-attachments/assets/52310e9f-38e3-44c0-a285-06e4dd1590a1)

`job_data` is a dictionary [generated by the method `_static_scrape_job`](https://github.com/canonical/prometheus-k8s-operator/blob/main/lib/charms/prometheus_k8s/v0/prometheus_scrape.py#L2003) that has the `static_configs` key:

![Image](https://github.com/user-attachments/assets/6933ec3d-da19-4fd9-a965-95691e39688c)

The [`ScrapeJobModel`](https://github.com/canonical/charm-relation-interfaces/blob/main/interfaces/prometheus_scrape/v0/schema.py#L100) class is pydantic `BaseModel` that looks like:

![Image](https://github.com/user-attachments/assets/cb52f8d3-e030-43ac-96b6-430eb500c6b1)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with src/charm.py at lines 309, 330, 639, and 664-665, then run tox -e static-charm to reproduce the Pyright errors. Read the referenced _static_scrape_job method and ScrapeJobModel schema to understand the reported types. Done means the static-charm check passes without the eight errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.