[RFC] Typing Red #2520

Closed
opened 2019-03-15 15:54:12 +01:00 by mikeshardmind · 1 comment
mikeshardmind commented 2019-03-15 15:54:12 +01:00 (Migrated from github.com)

I'd like to see Red be fully typed, or at least to type all of the public APIs.

Note: I do consider all public methods and attributes of core cogs as part of the public API given that it is currently neccessary to interact with other cogs for the modlog to not misbehave, as well as it being documented that bot.get_cog is an appropriate way to share resources.

Pros

  • Better IDE support
  • Quicker detection of errors
  • Better maintainability

Cons

  • Adds more things for people to need to keep correct.
  • There are some places in Red where a fully correct type hint is not easily added without extra dependencies (non-homogeonous dicts)

There are also places in Red where the typehints are currently incorrect.

For example, consider the below:

# https://github.com/Cog-Creators/Red-DiscordBot/blob/94c3a2feddb8c987f66727cd1efe7c1336d5e9cb/redbot/core/config.py#L877-L894
    def custom(self, group_identifier: str, *identifiers: str):
        """Returns a `Group` for the given custom group.

        Parameters
        ----------
        group_identifier : str
            Used to identify the custom group.
        identifiers : str
            The attributes necessary to uniquely identify an entry in the
            custom group. These are casted to `str` for you.

        Returns
        -------
        `Group <redbot.core.config.Group>`
            The custom group's Group object.

        """
        return self._get_base_group(str(group_identifier), *map(str, identifiers))

This function goes out of it's way to support anything castable to a string for user convieniece, so the type here is wrong, and many IDEs typehint support will complain about passing a non string.

Casting to a string before-hand is also possible, but that defeats the purpose of the convienience added here.

Switching the typehint to use something along the lines of the below is correct for the duck typing here, without being overly restrictive on what can be passed to the function. (see PEP 544)

from typing import Protocol

class SupportsStr(Protocol):
    def __str__(self) -> str:
        ...
I'd like to see Red be fully typed, or at least to type all of the public APIs. Note: I do consider all public methods and attributes of core cogs as part of the public API given that it is currently neccessary to interact with other cogs for the modlog to not misbehave, as well as it being documented that `bot.get_cog` is an appropriate way to share resources. ### Pros - Better IDE support - Quicker detection of errors - Better maintainability ### Cons - Adds more things for people to need to keep correct. - There are some places in Red where a fully correct type hint is not easily added without extra dependencies (non-homogeonous dicts) There are also places in Red where the typehints are currently incorrect. For example, consider the below: ```py # https://github.com/Cog-Creators/Red-DiscordBot/blob/94c3a2feddb8c987f66727cd1efe7c1336d5e9cb/redbot/core/config.py#L877-L894 def custom(self, group_identifier: str, *identifiers: str): """Returns a `Group` for the given custom group. Parameters ---------- group_identifier : str Used to identify the custom group. identifiers : str The attributes necessary to uniquely identify an entry in the custom group. These are casted to `str` for you. Returns ------- `Group <redbot.core.config.Group>` The custom group's Group object. """ return self._get_base_group(str(group_identifier), *map(str, identifiers)) ``` This function goes out of it's way to support anything castable to a string for user convieniece, so the type here is wrong, and many IDEs typehint support will complain about passing a non string. Casting to a string before-hand is also possible, but that defeats the purpose of the convienience added here. Switching the typehint to use something along the lines of the below is correct for the duck typing here, without being overly restrictive on what can be passed to the function. (see PEP 544) ``` from typing import Protocol class SupportsStr(Protocol): def __str__(self) -> str: ... ```
mikeshardmind commented 2020-01-04 06:31:52 +01:00 (Migrated from github.com)

Closing this, it's still an ongoing goal to improve, but we don't have any metrics for this we're using as a lot of our typing complexity is at the discord.py boundary and not easily resolved with discord.py supporting as low as python3.5.x

Closing this, it's still an ongoing goal to improve, but we don't have any metrics for this we're using as a lot of our typing complexity is at the discord.py boundary and not easily resolved with discord.py supporting as low as python3.5.x
Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot#2520
No description provided.