Skip to content

Fix typing of LetterCase #560

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mtnpke
Copy link

@mtnpke mtnpke commented Apr 29, 2025

Multiple issues prevented the letter_case argument on the dataclass_json decorator to work correctly with strict type checking settings and PyRight:

  • While config() etc. correctly accept also a callable instead of only an enum member, the dataclass_json decorator did not do this. Since the enum members are mapped to callables, which cannot be used as base types, they cannot be used as argument values of type LetterCase.
  • All functions in stringcase.py were missing type hints.
  • The LetterCase enum members need concrete type definitions, so that there arguments are sufficiently constrained and they are compatible with the correct callable type Callable[[str], str]. PyRight would otherwise infer Callable[..., str], which is not narrow enough.

These changes allow a JSON dataclass with letter_case argument to pass type testing with basedpyright. The code is fully backwards-compatible, as only type information is enhanced. Since dataclasses-json includes the py.typed marker file, it would be helpful if the type info was as complete and useful as it can be.

Test case:

from dataclasses import dataclass
from dataclasses_json import dataclass_json, LetterCase

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class Test:
    name: str

Result before:

$ uvx basedpyright pyrighttest.py
c:\_GIT\3rdparty\dataclasses-json\pyrighttest.py
  c:\_GIT\3rdparty\dataclasses-json\pyrighttest.py:4:29 - warning: Type of "CAMEL" is partially unknown
    Type of "CAMEL" is "(string: Unknown) -> (Unknown | Literal[''])" (reportUnknownMemberType)
  c:\_GIT\3rdparty\dataclasses-json\pyrighttest.py:4:29 - error: Argument of type "(string: Unknown) -> (Unknown | Literal[''])" cannot be assigned to parameter "letter_case" of type "LetterCase | None" in function "dataclass_json"
    Type "(string: Unknown) -> (Unknown | Literal[''])" is not assignable to type "LetterCase | None"
      "function" is not assignable to "LetterCase"
      "function" is not assignable to "None" (reportArgumentType)
1 error, 1 warning, 0 notes

Result after:

$ uvx basedpyright pyrighttest.py
0 errors, 0 warnings, 0 notes

Multiple issues prevented the letter_case argument on the dataclass_json
decorator to work correctly with strict type checking settings and
PyRight:
* While config() etc. correctly accept also a callable instead of only
  an enum member, the dataclass_json decorator did not do this. Since
  the enum members are mapped to callables, which cannot be used as base
  types, they cannot be used as argument values of type LetterCase.
* All functions in stringcase.py were missing type hints.
* The LetterCase enum members need concrete type definitions, so that
  there arguments are sufficiently constrained and they are compatible
  with the correct callable type `Callable[[str], str]`. PyRight would
  otherwise infer `Callable[..., str]`, which is not narrow enough.

These changes allow a JSON dataclass with letter_case argument to pass
type testing with basedpyright. The code is fully backwards-compatible,
as only type information is enhanced.
@mtnpke mtnpke force-pushed the lettercase-typing branch from e39c25f to 08266a6 Compare April 29, 2025 07:49
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.

1 participant