Skip to content

Infinite recursion with typing.get_type_hints #81987

Closed

Description

@vg0377467
mannequin
BPO 37806
Nosy @ilevkivskyi, @hongweipeng
PRs
  • bpo-37806:Fix infinite recursion with typing.get_type_hints. #15493
  • bpo-37806:Add check recursion in typeing.ForwardRef when get hash. #15559
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ilevkivskyi'
    closed_at = <Date 2019-09-14.05:07:37.939>
    created_at = <Date 2019-08-10.05:26:56.990>
    labels = ['3.8', 'library', 'type-crash']
    title = 'Infinite recursion with typing.get_type_hints'
    updated_at = <Date 2019-09-14.05:07:37.938>
    user = 'https://bugs.python.org/vg0377467'

    bugs.python.org fields:

    activity = <Date 2019-09-14.05:07:37.938>
    actor = 'matrixise'
    assignee = 'levkivskyi'
    closed = True
    closed_date = <Date 2019-09-14.05:07:37.939>
    closer = 'matrixise'
    components = ['Library (Lib)']
    creation = <Date 2019-08-10.05:26:56.990>
    creator = 'vg0377467'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37806
    keywords = ['patch']
    message_count = 3.0
    messages = ['349330', '352400', '352401']
    nosy_count = 3.0
    nosy_names = ['levkivskyi', 'hongweipeng', 'vg0377467']
    pr_nums = ['15493', '15559']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue37806'
    versions = ['Python 3.8']

    Activity

    1. vg0377467 commented on Aug 10, 2019

      vg0377467mannequin
      MannequinAuthor

      I encountered one condition where calling get_type_hints causes infinite recursion when dealing with forward declaration and cyclic types.

      Here's an example:

      from typing import Union, List, get_type_hints
      
      ValueList = List['Value']
      Value = Union[str, ValueList]
      
      class A:
          a: List[Value]

      get_type_hints(A, globals(), locals())
      This reaches the recursion limit as of 3.8.0b2.

      It seems that the combining _GenericAlias with ForwardRef is what triggers this condition:

      ForwardRef._evaluate sets __forward_value__ on its first call on a given instance
      _GenericAlias tries to compare its args post evaluation
      If one of the arguments is a previously evaluated forward reference containing a cycle, then it will infinitely recurse in the hash function when building a frozen set for the comparison.
      The above is, of course, a very artificial example, but I can imagine this happening a lot in code with trees or similar structures.
      My initial reproduction case was using _eval_type to resolve forward references returned by get_args (side note: it would be nice to have a public function to do that).

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Aug 10, 2019
    3. self-assigned this
      on Aug 10, 2019
    4. hongweipeng commented on Sep 14, 2019

      hongweipengmannequin
      Mannequin

      This report can be closed, PR bpo-15400 has solved this issue.

    5. hongweipeng commented on Sep 14, 2019

      hongweipengmannequin
      Mannequin

      Sorry, the link is #15400 .

    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions