Giter Site home page Giter Site logo

Comments (7)

peterschutt avatar peterschutt commented on May 23, 2024 1

You are more than welcome to make a PR, cheers.

To make it easier to review I'd first suggest creating a minimal example (I'm talking about something we can copy and paste and run without any extra work) that demonstrates the issue against main. Then we have some context when reviewing the PR.

from litestar.

peterschutt avatar peterschutt commented on May 23, 2024

We have

def get_type_hints_with_generics_resolved(
annotation: Any,
globalns: dict[str, Any] | None = None,
localns: dict[str, Any] | None = None,
include_extras: bool = False,
type_hints: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Get the type hints for the given object after resolving the generic types as much as possible.
Args:
annotation: A type annotation.
globalns: The global namespace.
localns: The local namespace.
include_extras: A flag indicating whether to include the ``Annotated[T, ...]`` or not.
type_hints: Already resolved type hints
"""
origin = get_origin(annotation)
if origin is None:
# Implies the generic types have not been specified in the annotation
if type_hints is None: # pragma: no cover
type_hints = get_type_hints(annotation, globalns=globalns, localns=localns, include_extras=include_extras)
typevar_map = {p: p for p in annotation.__parameters__}
else:
if type_hints is None: # pragma: no cover
type_hints = get_type_hints(origin, globalns=globalns, localns=localns, include_extras=include_extras)
# the __parameters__ is only available on the origin itself and not the annotation
typevar_map = dict(zip(origin.__parameters__, get_args(annotation)))
return {n: _substitute_typevars(type_, typevar_map) for n, type_ in type_hints.items()}

from litestar.

haryle avatar haryle commented on May 23, 2024

We have

def get_type_hints_with_generics_resolved(
annotation: Any,
globalns: dict[str, Any] | None = None,
localns: dict[str, Any] | None = None,
include_extras: bool = False,
type_hints: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Get the type hints for the given object after resolving the generic types as much as possible.
Args:
annotation: A type annotation.
globalns: The global namespace.
localns: The local namespace.
include_extras: A flag indicating whether to include the ``Annotated[T, ...]`` or not.
type_hints: Already resolved type hints
"""
origin = get_origin(annotation)
if origin is None:
# Implies the generic types have not been specified in the annotation
if type_hints is None: # pragma: no cover
type_hints = get_type_hints(annotation, globalns=globalns, localns=localns, include_extras=include_extras)
typevar_map = {p: p for p in annotation.__parameters__}
else:
if type_hints is None: # pragma: no cover
type_hints = get_type_hints(origin, globalns=globalns, localns=localns, include_extras=include_extras)
# the __parameters__ is only available on the origin itself and not the annotation
typevar_map = dict(zip(origin.__parameters__, get_args(annotation)))
return {n: _substitute_typevars(type_, typevar_map) for n, type_ in type_hints.items()}

I see, but fn_type_hints no longer contains the expanded type information after line 214. In the previous example, fn_type_hint = {'data': ~T, 'return': ~T}

fn_type_hints = get_fn_type_hints(fn, namespace=signature_namespace)

from litestar.

peterschutt avatar peterschutt commented on May 23, 2024

Not sure what you mean. I'm just pointing out that we have a utility that does type get_type_hints() with type var expansion.

from litestar.

haryle avatar haryle commented on May 23, 2024

Sorry, I wasn't clear. Correct me if I am wrong, but I don't think that utility method gets called in the ParsedSignature.from_fn call stack.

from litestar.

peterschutt avatar peterschutt commented on May 23, 2024

Obviously it was me who was unclear. I wasn't saying it did - just pointing out we have the utility you mentioned which maybe we can use. LMK how it goes.

from litestar.

haryle avatar haryle commented on May 23, 2024

Ah thank you. Should I make a PR after adding it in? Sorry I am quite new to contributing to open source project.

from litestar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.