Skip to content

Fix a couple type annotations in the RootConfig/Config #18409

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

Merged
merged 7 commits into from
May 13, 2025

Conversation

MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented May 7, 2025

Fix a couple type annotations in the RootConfig/Config. Discovered while cribbing this code for another project.

It's really sucks that mypy type checking doesn't catch this. I assume this is because we also have a synapse/config/_base.pyi that overrides all of this. Still unclear to me why the Iterable[str] vs StrSequence issue wasn't caught as that's what ConfigError expects.

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct
    (run the linters)

@@ -1047,7 +1047,7 @@ def get_instance(self, key: str) -> str:
return self._get_instance(key)


def read_file(file_path: Any, config_path: Iterable[str]) -> str:
def read_file(file_path: Any, config_path: StrSequence) -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated because error: Argument 2 to "ConfigError" has incompatible type "Iterable[str]"; expected "Union[tuple[str, ...], list[str], None]" [arg-type]

(ConfigError is used below)

Updating this also catches a few mistakes in our codebase.

@@ -445,7 +445,7 @@ def invoke_all(
return res

@classmethod
def invoke_all_static(cls, func_name: str, *args: Any, **kwargs: any) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a typo. any isn't a valid type (correct type is Any)

I assume this wasn't caught because it's defined correctly in the stub file _base.pyi

def invoke_all_static(cls, func_name: str, *args: Any, **kwargs: Any) -> None: ...

See #18409 (comment)

> The constructor accepts `Optional[RootConfig]` but the attribute is not
> optional `root: RootConfig` and we don't default it when not passed in. We
> either need to a) assert is not `None` or b) adjust the function signature
> to not allow `Optional`
@@ -170,7 +170,7 @@ class Config:

section: ClassVar[str]

def __init__(self, root_config: "RootConfig" = None):
def __init__(self, root_config: "RootConfig"):
Copy link
Contributor Author

@MadLittleMods MadLittleMods May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our code assumes this isn't Optional everywhere. And we always pass it in as far as I can tell. The only place we didn't do this was in the tests but I've updated all of them to pass in a RootConfig.

synapse/config/_base.pyi also assumes it isn't Optional:

class Config:
root: RootConfig

@@ -0,0 +1 @@
Fix a couple type annotations in the `RootConfig`/`Config`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complement CI failures are unrelated to this PR and tracked by matrix-org/complement#775

@MadLittleMods MadLittleMods marked this pull request as ready for review May 7, 2025 22:58
@MadLittleMods MadLittleMods requested a review from a team as a code owner May 7, 2025 22:58
@MadLittleMods MadLittleMods merged commit 6e910e2 into develop May 13, 2025
39 checks passed
@MadLittleMods MadLittleMods deleted the madlittlemods/type-annotation-fix-on-config branch May 13, 2025 15:22
@MadLittleMods
Copy link
Contributor Author

Thanks for the review @erikjohnston 🐝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants