# Buggy code
def from_type(thing):
    """Looks up the appropriate search strategy for the given type.

    ``from_type`` is used internally to fill in missing arguments to
    :func:`~hypothesis.strategies.builds` and can be used interactively
    to explore what strategies are available or to debug type resolution.

    You can use :func:`~hypothesis.strategies.register_type_strategy` to
    handle your custom types, or to globally redefine certain strategies -
    for example excluding NaN from floats, or use timezone-aware instead of
    naive time and datetime strategies.

    The resolution logic may be changed in a future version, but currently
    tries these four options:

    1. If ``thing`` is in the default lookup mapping or user-registered lookup,
       return the corresponding strategy.  The default lookup covers all types
       with Hypothesis strategies, including extras where possible.
    2. If ``thing`` is from the :mod:`python:typing` module, return the
       corresponding strategy (special logic).
    3. If ``thing`` has one or more subtypes in the merged lookup, return
       the union of the strategies for those types that are not subtypes of
       other elements in the lookup.
    4. Finally, if ``thing`` has type annotations for all required arguments,
       it is resolved via :func:`~hypothesis.strategies.builds`.

    """
    from hypothesis.searchstrategy import types

    if not isinstance(thing, type):
        # Under Python 3.6, Unions are not instances of `type` - but we still
        # want to resolve them!  This __origin__ check only passes if thing is
        # a Union with parameters; if it doesn't we can't resolve it anyway.
        try:
            import typing

            if getattr(thing, "__origin__", None) is typing.Union:
                args = sorted(thing.__args__, key=types.type_sorting_key)
                return one_of([from_type(t) for t in args])
        except ImportError:  # pragma: no cover
            pass
        raise InvalidArgument("thing=%s must be a type" % (thing,))
    # Now that we know `thing` is a type, the first step is to check for an
    # explicitly registered strategy.  This is the best (and hopefully most
    # common) way to resolve a type to a strategy.  Note that the value in the
    # lookup may be a strategy or a function from type -> strategy; and we
    # convert empty results into an explicit error.
    if thing in types._global_type_lookup:
        strategy = types._global_type_lookup[thing]
        if not isinstance(strategy, SearchStrategy):
            strategy = strategy(thing)
        if strategy.is_empty:
            raise ResolutionFailed("Error: %r resolved to an empty strategy" % (thing,))
        return strategy
    # If there's no explicitly registered strategy, maybe a subtype of thing
    # is registered - if so, we can resolve it to the subclass strategy.
    # We'll start by checking if thing is from from the typing module,
    # because there are several special cases that don't play well with
    # subclass and instance checks.
    try:
        import typing

        if isinstance(thing, typing.TypingMeta):
            return types.from_typing_type(thing)
    except ImportError:  # pragma: no cover
        pass
    # If it's not from the typing module, we get all registered types that are
    # a subclass of `thing` and are not themselves a subtype of any other such
    # type.  For example, `Number -> integers() | floats()`, but bools() is
    # not included because bool is a subclass of int as well as Number.
    strategies = [
        v if isinstance(v, SearchStrategy) else v(thing)
        for k, v in types._global_type_lookup.items()
        if issubclass(k, thing)
        and sum(types.try_issubclass(k, T) for T in types._global_type_lookup) == 1
    ]
    empty = ", ".join(repr(s) for s in strategies if s.is_empty)
    if empty:
        raise ResolutionFailed(
            "Could not resolve %s to a strategy; consider using "
            "register_type_strategy" % empty
        )
    elif strategies:
        return one_of(strategies)
    # If we don't have a strategy registered for this type or any subtype, we
    # may be able to fall back on type annotations.
    # Types created via typing.NamedTuple use a custom attribute instead -
    # but we can still use builds(), if we work out the right kwargs.
    if (
        issubclass(thing, tuple)
        and hasattr(thing, "_fields")
        and hasattr(thing, "_field_types")
    ):
        kwargs = {k: from_type(thing._field_types[k]) for k in thing._fields}
        return builds(thing, **kwargs)
    # If the constructor has an annotation for every required argument,
    # we can (and do) use builds() without supplying additional arguments.
    required = required_args(thing)
    if not required or required.issubset(get_type_hints(thing.__init__)):
        return builds(thing)
    # We have utterly failed, and might as well say so now.
    raise ResolutionFailed(
        "Could not resolve %r to a strategy; consider "
        "using register_type_strategy" % (thing,)
    )


# Traceback (KeyError)
In [14]: st.from_type(T).example()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/strategies.py in cached_strategy(*args, **kwargs)
104         try:
--> 105             return STRATEGY_CACHE[cache_key]
106         except TypeError:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/cache.py in __getitem__(self, key)
69     def __getitem__(self, key):
---> 70         i = self.keys_to_indices[key]
71         result = self.data[i]

KeyError: (<function from_type at 0x106c740d0>, ((<class 'type'>, <class '__main__.T'>),), frozenset())

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/strategies.py in cached_strategy(*args, **kwargs)
104         try:
--> 105             return STRATEGY_CACHE[cache_key]
106         except TypeError:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/cache.py in __getitem__(self, key)
69     def __getitem__(self, key):
---> 70         i = self.keys_to_indices[key]
71         result = self.data[i]

KeyError: (<function from_type at 0x106c740d0>, ((<class 'function'>, <function NewType.<locals>.new_type at 0x107fd40d0>),), frozenset())

During handling of the above exception, another exception occurred:

InvalidArgument                           Traceback (most recent call last)
<ipython-input-14-87157b77bd3c> in <module>()
----> 1 st.from_type(T).example()

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/strategies.py in example(self, random)
284                     max_iterations=1000,
285                     database=None,
--> 286                     verbosity=Verbosity.quiet,
287                 )
288             )

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/core.py in find(specifier, condition, settings, random, database_key)
1067         database_key=database_key,
1068     )
-> 1069     runner.run()
1070     note_engine_for_statistics(runner)
1071     run_time = time.time() - start

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/engine.py in run(self)
380         with self.settings:
381             try:
--> 382                 self._run()
383             except RunIsComplete:
384                 pass

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/engine.py in _run(self)
754
755         self.reuse_existing_examples()
--> 756         self.generate_new_examples()
757
758         if (

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/engine.py in generate_new_examples(self)
655             draw_bytes=lambda data, n: self.__rewrite_for_novelty(
656                 data, hbytes(n)))
--> 657         self.test_function(zero_data)
658
659         count = 0

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/engine.py in test_function(self, data)
122         self.call_count += 1
123         try:
--> 124             self._test_function(data)
125             data.freeze()
126         except StopTest as e:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/core.py in template_condition(data)
1036             try:
1037                 data.is_find = True
-> 1038                 result = data.draw(search)
1039                 data.note(result)
1040                 success = condition(result)

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in draw(self, strategy)
123             try:
124                 sys.settrace(None)
--> 125                 return self.__draw(strategy)
126             finally:
127                 sys.settrace(original_tracer)

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in __draw(self, strategy)
132         self.start_example()
133         try:
--> 134             return strategy.do_draw(self)
135         finally:
136             if not self.frozen:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/lazy.py in do_draw(self, data)
165
166     def do_draw(self, data):
--> 167         return data.draw(self.wrapped_strategy)

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in draw(self, strategy)
127                 sys.settrace(original_tracer)
128         else:
--> 129             return self.__draw(strategy)
130
131     def __draw(self, strategy):

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in __draw(self, strategy)
132         self.start_example()
133         try:
--> 134             return strategy.do_draw(self)
135         finally:
136             if not self.frozen:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/strategies.py in do_draw(self, data)
505             i = data.index
506             try:
--> 507                 return self.pack(data.draw(self.mapped_strategy))
508             except UnsatisfiedAssumption:
509                 if data.index == i:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in draw(self, strategy)
127                 sys.settrace(original_tracer)
128         else:
--> 129             return self.__draw(strategy)
130
131     def __draw(self, strategy):

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in __draw(self, strategy)
132         self.start_example()
133         try:
--> 134             return strategy.do_draw(self)
135         finally:
136             if not self.frozen:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/lazy.py in do_draw(self, data)
165
166     def do_draw(self, data):
--> 167         return data.draw(self.wrapped_strategy)

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in draw(self, strategy)
127                 sys.settrace(original_tracer)
128         else:
--> 129             return self.__draw(strategy)
130
131     def __draw(self, strategy):

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in __draw(self, strategy)
132         self.start_example()
133         try:
--> 134             return strategy.do_draw(self)
135         finally:
136             if not self.frozen:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/collections.py in do_draw(self, data)
58     def do_draw(self, data):
59         return self.newtuple(
---> 60             data.draw(e) for e in self.element_strategies
61         )
62

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/collections.py in newtuple(self, xs)
54     def newtuple(self, xs):
55         """Produce a new tuple of the correct type."""
---> 56         return tuple(xs)
57
58     def do_draw(self, data):

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/collections.py in <genexpr>(.0)
58     def do_draw(self, data):
59         return self.newtuple(
---> 60             data.draw(e) for e in self.element_strategies
61         )
62

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in draw(self, strategy)
127                 sys.settrace(original_tracer)
128         else:
--> 129             return self.__draw(strategy)
130
131     def __draw(self, strategy):

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in __draw(self, strategy)
132         self.start_example()
133         try:
--> 134             return strategy.do_draw(self)
135         finally:
136             if not self.frozen:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/strategies.py in do_draw(self, data)
505             i = data.index
506             try:
--> 507                 return self.pack(data.draw(self.mapped_strategy))
508             except UnsatisfiedAssumption:
509                 if data.index == i:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in draw(self, strategy)
127                 sys.settrace(original_tracer)
128         else:
--> 129             return self.__draw(strategy)
130
131     def __draw(self, strategy):

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in __draw(self, strategy)
132         self.start_example()
133         try:
--> 134             return strategy.do_draw(self)
135         finally:
136             if not self.frozen:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/collections.py in do_draw(self, data)
58     def do_draw(self, data):
59         return self.newtuple(
---> 60             data.draw(e) for e in self.element_strategies
61         )
62

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/collections.py in newtuple(self, xs)
54     def newtuple(self, xs):
55         """Produce a new tuple of the correct type."""
---> 56         return tuple(xs)
57
58     def do_draw(self, data):

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/collections.py in <genexpr>(.0)
58     def do_draw(self, data):
59         return self.newtuple(
---> 60             data.draw(e) for e in self.element_strategies
61         )
62

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in draw(self, strategy)
127                 sys.settrace(original_tracer)
128         else:
--> 129             return self.__draw(strategy)
130
131     def __draw(self, strategy):

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/internal/conjecture/data.py in __draw(self, strategy)
132         self.start_example()
133         try:
--> 134             return strategy.do_draw(self)
135         finally:
136             if not self.frozen:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/searchstrategy/strategies.py in do_draw(self, data)
505             i = data.index
506             try:
--> 507                 return self.pack(data.draw(self.mapped_strategy))
508             except UnsatisfiedAssumption:
509                 if data.index == i:

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/strategies.py in <lambda>(value)
871         kwargs[ms] = from_type(hints[ms])
872     return tuples(tuples(*args), fixed_dictionaries(kwargs)).map(
--> 873         lambda value: target(*value[0], **value[1])
874     )
875

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/strategies.py in lazy_error()
891
892             def lazy_error():
--> 893                 raise error
894
895             return builds(lazy_error)

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/strategies.py in inner(*args, **kwargs)
886     def inner(*args, **kwargs):
887         try:
--> 888             return func(*args, **kwargs)
889         except Exception as e:
890             error = e

/Users/daenyth/.pyenv/versions/3.6.1/envs/obs-venv/lib/python3.6/site-packages/hypothesis/strategies.py in from_type(thing)
938         except ImportError:  # pragma: no cover
939             pass
--> 940         raise InvalidArgument('thing=%s must be a type' % (thing,))
941     # Now that we know `thing` is a type, the first step is to check for an
942     # explicitly registered strategy.  This is the best (and hopefully most

InvalidArgument: thing=<function NewType.<locals>.new_type at 0x107fd40d0> must be a type

# Fixed code
def from_type(thing):
    """Looks up the appropriate search strategy for the given type.

    ``from_type`` is used internally to fill in missing arguments to
    :func:`~hypothesis.strategies.builds` and can be used interactively
    to explore what strategies are available or to debug type resolution.

    You can use :func:`~hypothesis.strategies.register_type_strategy` to
    handle your custom types, or to globally redefine certain strategies -
    for example excluding NaN from floats, or use timezone-aware instead of
    naive time and datetime strategies.

    The resolution logic may be changed in a future version, but currently
    tries these four options:

    1. If ``thing`` is in the default lookup mapping or user-registered lookup,
       return the corresponding strategy.  The default lookup covers all types
       with Hypothesis strategies, including extras where possible.
    2. If ``thing`` is from the :mod:`python:typing` module, return the
       corresponding strategy (special logic).
    3. If ``thing`` has one or more subtypes in the merged lookup, return
       the union of the strategies for those types that are not subtypes of
       other elements in the lookup.
    4. Finally, if ``thing`` has type annotations for all required arguments,
       it is resolved via :func:`~hypothesis.strategies.builds`.

    """
    from hypothesis.searchstrategy import types

    if not isinstance(thing, type):
        try:
            # At runtime, `typing.NewType` returns an identity function rather
            # than an actual type, but we can check that for a possible match
            # and then read the magic attribute to unwrap it.
            import typing

            if all(
                [
                    hasattr(thing, "__supertype__"),
                    hasattr(typing, "NewType"),
                    isfunction(thing),
                    getattr(thing, "__module__", 0) == "typing",
                ]
            ):
                return from_type(thing.__supertype__)
            # Under Python 3.6, Unions are not instances of `type` - but we
            # still want to resolve them!
            if getattr(thing, "__origin__", None) is typing.Union:
                args = sorted(thing.__args__, key=types.type_sorting_key)
                return one_of([from_type(t) for t in args])
        except ImportError:  # pragma: no cover
            pass
        raise InvalidArgument("thing=%s must be a type" % (thing,))
    # Now that we know `thing` is a type, the first step is to check for an
    # explicitly registered strategy.  This is the best (and hopefully most
    # common) way to resolve a type to a strategy.  Note that the value in the
    # lookup may be a strategy or a function from type -> strategy; and we
    # convert empty results into an explicit error.
    if thing in types._global_type_lookup:
        strategy = types._global_type_lookup[thing]
        if not isinstance(strategy, SearchStrategy):
            strategy = strategy(thing)
        if strategy.is_empty:
            raise ResolutionFailed("Error: %r resolved to an empty strategy" % (thing,))
        return strategy
    # If there's no explicitly registered strategy, maybe a subtype of thing
    # is registered - if so, we can resolve it to the subclass strategy.
    # We'll start by checking if thing is from from the typing module,
    # because there are several special cases that don't play well with
    # subclass and instance checks.
    try:
        import typing

        if isinstance(thing, typing.TypingMeta):
            return types.from_typing_type(thing)
    except ImportError:  # pragma: no cover
        pass
    # If it's not from the typing module, we get all registered types that are
    # a subclass of `thing` and are not themselves a subtype of any other such
    # type.  For example, `Number -> integers() | floats()`, but bools() is
    # not included because bool is a subclass of int as well as Number.
    strategies = [
        v if isinstance(v, SearchStrategy) else v(thing)
        for k, v in types._global_type_lookup.items()
        if issubclass(k, thing)
        and sum(types.try_issubclass(k, T) for T in types._global_type_lookup) == 1
    ]
    empty = ", ".join(repr(s) for s in strategies if s.is_empty)
    if empty:
        raise ResolutionFailed(
            "Could not resolve %s to a strategy; consider using "
            "register_type_strategy" % empty
        )
    elif strategies:
        return one_of(strategies)
    # If we don't have a strategy registered for this type or any subtype, we
    # may be able to fall back on type annotations.
    # Types created via typing.NamedTuple use a custom attribute instead -
    # but we can still use builds(), if we work out the right kwargs.
    if (
        issubclass(thing, tuple)
        and hasattr(thing, "_fields")
        and hasattr(thing, "_field_types")
    ):
        kwargs = {k: from_type(thing._field_types[k]) for k in thing._fields}
        return builds(thing, **kwargs)
    # If the constructor has an annotation for every required argument,
    # we can (and do) use builds() without supplying additional arguments.
    required = required_args(thing)
    if not required or required.issubset(get_type_hints(thing.__init__)):
        return builds(thing)
    # We have utterly failed, and might as well say so now.
    raise ResolutionFailed(
        "Could not resolve %r to a strategy; consider "
        "using register_type_strategy" % (thing,)
    )