Skip to content

resolve_types while structuring generic classes #718

@AdrianSosic

Description

@AdrianSosic

Hi @Tinche, I just ran into a situation that I don't really understand. Also, I'm not sure if it's:

  • a problem with attrs
  • a problem with cattrs
  • a problem with the typing module
  • or actually not a problem at all and I'm just using stuff incorrectly 😬

But since I encountered it during structuring classes using cattrs, I thought I'd report here and hope for you help.

The situation that I'm facing is essentially this one:

from typing import Generic, Protocol, TypeVar

import attrs

T = TypeVar("T")


class ProtocolClass(Protocol): ...


@attrs.define
class GenericClass(Generic[T]): ...


attrs.resolve_types(GenericClass[ProtocolClass])

which gives

TypeError: __main__.GenericClass[__main__.ProtocolClass] is not a module, class, method, or function.

It's basically a minimal example extracted from our production system where structuring happens behind an API. However, the interesting thing is: When I run the structuring operations that trigger this logic from a different context on my local machine, I'm not getting any errors. More precisely: whether the error occurs or not depends on whether I use a fresh python session (--> error is there) vs run structuring in the same session where the unstructured representation was created (--> error is not there). So it seems that in the latter case, the types are already resolved and resolution somehow took a different execution path that didn't cause problems.

Do you have any clue what could be going on here? In particular, is the above code supposed to run or throw an error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions