Skip to content

stub - fix issues with literal types #1373

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 1 commit into from
May 8, 2025

Conversation

Andrej730
Copy link
Contributor

See example snippet - because of the issue with literal types commit.type type is detected as 'Unknown'.
image

import pygit2
repo = pygit2.Repository(r"L:\test")
for commit in repo.walk(repo.head.target, pygit2.GIT_SORT_TIME):
    print(commit.type) # type detected by type checker is 'Unknown'.
    print(commit.message)
    break

After this fix the type becomes Literal[1, 2, 4, 3].
image

It also resolves the issue with a couple overloads that use those types. E.g. running pyright on _pygit2.pyi before this fix:

pyright L:\Projects\Github\pygit2\pygit2\_pygit2.pyi
l:\Projects\Github\pygit2\pygit2\_pygit2.pyi
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:45:20 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:45:20 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:45:46 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:45:46 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:45:70 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:45:70 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:45:93 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:45:93 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:48:9 - error: Overload 1 for "peel" overlaps overload 2 and returns an incompatible type (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:48:9 - error: Overload 1 for "peel" overlaps overload 3 and returns an incompatible type (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:48:9 - error: Overload 1 for "peel" overlaps overload 4 and returns an incompatible type (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:48:42 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:48:42 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:50:9 - error: Overload 2 for "peel" will never be used because its parameters overlap overload 1 (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:50:42 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:50:42 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:52:9 - error: Overload 3 for "peel" will never be used because its parameters overlap overload 1 (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:52:42 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:52:42 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:54:9 - error: Overload 4 for "peel" will never be used because its parameters overlap overload 1 (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:54:42 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:54:42 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:56:9 - error: Overload 5 for "peel" will never be used because its parameters overlap overload 1 (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:78:9 - error: Overload 1 for "peel" overlaps overload 2 and returns an incompatible type (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:78:9 - error: Overload 1 for "peel" overlaps overload 3 and returns an incompatible type (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:78:9 - error: Overload 1 for "peel" overlaps overload 4 and returns an incompatible type (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:78:35 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:78:35 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:80:9 - error: Overload 2 for "peel" will never be used because its parameters overlap overload 1 (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:80:35 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:80:35 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:82:9 - error: Overload 3 for "peel" will never be used because its parameters overlap overload 1 (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:82:35 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:82:35 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:84:9 - error: Overload 4 for "peel" will never be used because its parameters overlap overload 1 (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:84:35 - error: Variable not allowed in type expression (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:84:35 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:86:9 - error: Overload 5 for "peel" will never be used because its parameters overlap overload 1 (reportOverlappingOverload)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:127:9 - error: Method "rename" overrides class "Reference" in an incompatible manner
    Parameter 2 name mismatch: base parameter is named "new_name", override parameter is named "name" (reportIncompatibleMethodOverride)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:500:5 - error: "name" overrides symbol of same name in class "Object"
    Variable is mutable so its type is invariant
      Override type "str" is not the same as base type "str | None" (reportIncompatibleVariableOverride)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:502:5 - error: "raw_name" overrides symbol of same name in class "Object"
    Variable is mutable so its type is invariant
      Override type "bytes" is not the same as base type "bytes | None" (reportIncompatibleVariableOverride)
41 errors, 0 warnings, 0 informations

After the fix:

pyright L:\Projects\Github\pygit2\pygit2\_pygit2.pyi
l:\Projects\Github\pygit2\pygit2\_pygit2.pyi
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:127:9 - error: Method "rename" overrides class "Reference" in an incompatible manner
    Parameter 2 name mismatch: base parameter is named "new_name", override parameter is named "name" (reportIncompatibleMethodOverride)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:500:5 - error: "name" overrides symbol of same name in class "Object"
    Variable is mutable so its type is invariant
      Override type "str" is not the same as base type "str | None" (reportIncompatibleVariableOverride)
  l:\Projects\Github\pygit2\pygit2\_pygit2.pyi:502:5 - error: "raw_name" overrides symbol of same name in class "Object"
    Variable is mutable so its type is invariant
      Override type "bytes" is not the same as base type "bytes | None" (reportIncompatibleVariableOverride)
3 errors, 0 warnings, 0 informations

@jdavid jdavid merged commit fdb927e into libgit2:master May 8, 2025
8 checks passed
@Andrej730 Andrej730 deleted the stub-fix-literals branch May 8, 2025 15:38
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