OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can Pylance get the type annotation of a dynamically defined class?

  • Thread starter Thread starter phi friday
  • Start date Start date
P

phi friday

Guest
Code:
from types import ModuleType
from typing import Any, Dict, Tuple, Type, TypeVar

from . import default

_T = TypeVar("_T", bound=type)

class settings_meta(type):
    def __new__(
        cls: Type[_T],
        name: str,
        bases: Tuple[type, ...],
        namespace: Dict[str, Any],
        settings: ModuleType,
        **kwargs: Any,
    ) -> _T:
        from inspect import ismodule

        namespace = {
            key: val
            for key, val in vars(settings).items()
            if not key.startswith("__") and not ismodule(val)
        }
        namespace["__annotations__"] = {
            key: val
            for key, val in vars(settings)["__annotations__"].items()
            if key in namespace.keys()
        }
        return type.__new__(cls, name, bases, namespace, **kwargs)

    def __call__(self, *args: Any, **kwrags: Any) -> Any:
        raise RuntimeError

class settings(metaclass=settings_meta, settings=default):
    ...

I want to use settings class. But Pylance informs me of reportGeneralTypeIssues.

Because annotations has been called, but Pylance does not recognize it.

I know that this is simply solved by turning it off. Python › Analysis: Type Checking Mode

But I don't want to turn it off.

How can Pylance get the type annotation of a dynamically defined class?
<pre class="lang-py prettyprint-override"><code>from types import ModuleType
from typing import Any, Dict, Tuple, Type, TypeVar

from . import default

_T = TypeVar("_T", bound=type)

class settings_meta(type):
def __new__(
cls: Type[_T],
name: str,
bases: Tuple[type, ...],
namespace: Dict[str, Any],
settings: ModuleType,
**kwargs: Any,
) -> _T:
from inspect import ismodule

namespace = {
key: val
for key, val in vars(settings).items()
if not key.startswith("__") and not ismodule(val)
}
namespace["__annotations__"] = {
key: val
for key, val in vars(settings)["__annotations__"].items()
if key in namespace.keys()
}
return type.__new__(cls, name, bases, namespace, **kwargs)

def __call__(self, *args: Any, **kwrags: Any) -> Any:
raise RuntimeError

class settings(metaclass=settings_meta, settings=default):
...
</code></pre>
<p>I want to use <code>settings</code> class. But Pylance informs me of <code>reportGeneralTypeIssues</code>.</p>
<p>Because annotations has been called, but Pylance does not recognize it.</p>
<p>I know that this is simply solved by turning it off. <em>Python › Analysis: Type Checking Mode</em></p>
<p>But I don't want to turn it off.</p>
<p>How can Pylance get the type annotation of a dynamically defined class?</p>
 

Latest posts

G
Replies
0
Views
1
Gamal Othman
G
Top