Skip to content
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

Specific single dispatch crashes #11087

Closed
antonagestam opened this issue Sep 10, 2021 · 3 comments
Closed

Specific single dispatch crashes #11087

antonagestam opened this issue Sep 10, 2021 · 3 comments
Labels

Comments

@antonagestam
Copy link
Contributor

antonagestam commented Sep 10, 2021

Crash Report

Specific single dispatch crashes on mypy 0.910. I've tried breaking the code out to reproduce but that doesn't trigger the error.

Traceback

Traceback (most recent call last):
  File "/app/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/app/venv/lib/python3.9/site-packages/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 87, in main
  File "mypy/main.py", line 165, in run_build
  File "mypy/build.py", line 179, in build
  File "mypy/build.py", line 254, in _build
  File "mypy/build.py", line 2697, in dispatch
  File "mypy/build.py", line 3021, in process_graph
  File "mypy/build.py", line 3138, in process_stale_scc
  File "mypy/build.py", line 2288, in write_cache
  File "mypy/build.py", line 1476, in write_cache
  File "mypy/build.py", line 1428, in json_dumps
  File "/usr/local/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/local/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/lib/python3.9/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type AnyType is not JSON serializable

To Reproduce

Update: The below probably has nothing to do with this error. I believe the crash occurs only when there are no errors to report by mypy. This is from a large sized project and pinning down the code that causes this will be challenging ...

This function used to be typed like this which passed on mypy 0.812 (even though I realize accuracy might not have been as high as I expected):

T = TypeVar("T", bound=object)

@singledispatch
def serialize(value: T, _config: SerializerConfig) -> T:
    return value

@serialize.register  # Followed by a bunch of implementations.
def _serialize_enum(value: enum.Enum, _config: SerializerConfig) -> str: ...

But when upgrading to 0.910 this started giving errors like this. I don't know if it is a new requirement or not that the return value of registered functions are subtypes of the return value of the "main" function?

src/utils/schema.py:73: error: Argument 1 to "register" of "_SingleDispatchCallable" has incompatible type "Callable[[Enum, SerializerConfig], str]"; expected "Callable[..., T]"  [arg-type]
    @serialize.register

So to remedy I tried altering the function to this which gives triggers the error (I've also tried with Any and an internal JSONSerializable type which give the same result):

@singledispatch
def serialize(value: object, _config: SerializerConfig) -> object:
    return value

I've tried replacing the function and all its definitions with just def serialize(*args, **kwargs): ... and that makes the error go away.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
    [mypy]
    python_version = 3.9
    pretty = True
    files = src
    
    show_error_codes = True
    no_implicit_reexport = True
    no_implicit_optional = True
    strict_equality = True
    strict_optional = True
    check_untyped_defs = True
    disallow_incomplete_defs = True
    disallow_untyped_defs = True
    ignore_missing_imports = False
    
    warn_unused_configs = True
    warn_redundant_casts = True
    warn_unused_ignores = True
    warn_unreachable = True
    
  • Python version used: 3.9.6
  • Operating system and version: Both Macos 11.5.2 locally and in Docker image python:3.9.6-buster in CI.
@antonagestam
Copy link
Contributor Author

Hmm, actually replacing the function doesn't make the error go away, and I'm starting to doubt if it's even related to this module. It might be that the error "always" occurs when there are no type errors to report ...

@keithhackbarth
Copy link

I also have this issue. See similar chain of reports here: typeddjango/django-stubs#700

@antonagestam
Copy link
Contributor Author

@keithasaurus Ah, thanks for pointing that out, the suggested workaround there seems to fix this.

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

No branches or pull requests

2 participants