-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Segfault when working with TypeQuery #9001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Mypy sets a higher recursion limit, which may be triggering this (if there is more recursion than the system stack depth) then mypy can segfault. Can you try setting a higher stack limit? |
I have tried to add import sys
sys.setrecursionlimit(15000) into the plugin code. Nothing changed. |
@sobolevn if you are doing something that triggers infinite recursion that probably won't help. You need to set the ulimit (assuming you are on Linux) for the stack higher. Also your example plugin is non-functional (you don't define |
@ethanhs Thanks a lot for the help! I have updated my plugin definition. But, you could probably reproduce this with any 3rd-party plugin. This happens to me in both And about recursion size, I am not sure that this is the case, because |
There doesn't seem to be any reason from the traceback to think this is related to the recursion limit. More likely it's a mypyc bug. |
@JelleZijlstra at this point I'd tend to agree, but I've noticed that tracebacks don't always show everything going wrong, and I know we've seen segfaults from stack overflows before. Sounds like @msullivan would be the best to debug this further. |
Yeah I'll try to take a look. |
The same happens with Here's the code that might help: from mypy.erasetype import erase_type
from mypy.types import TypeVisitor, AnyType, TypeOfAny, get_proper_type
class KindSolver(TypeVisitor):
def visit_type_var(self, t):
return AnyType(TypeOfAny.special_form)
def test(ctx: FunctionContext) -> MypyType:
print(erase_type(ctx.default_return_type)) # existing mypy code works fine
print(ctx.default_return_type.accept(KindSolver())) # custom visitors fail with segfault
return ctx.default_return_type Output:
Basically, all |
The reverse operation print(KindSolver().visit_any(AnyType(TypeOfAny.special_form))) Output:
But, when there's a call to another print(KindSolver().visit_callable_type(ctx.default_return_type)) # segfault :( |
One more thing: x = Type()
print(x.accept(TypeVisitor())) Output:
|
Any ideas where to go next? I would be happy to help. Because this issue blocks the release of Higher Kinded Types I made. Sneak peak: @kinded
def map_functor(
f: Kind[K, T],
function: Callable[[T], V],
) -> Kind[K, V]:
return f.map(function)
reveal_type(map_functor(Maybe(1), str)) Outputs: The only thing left is to translate |
It would be helpful if the crash can be narrowed down to a smaller example that can be compiled separately with mypyc. I.e., we'd need a simplified version of |
I have tried different setups, nothing helps to reproduce it. |
Ok, here's something. When a class marked as I am not sure if this is related or not: # bug.py
from typing import Generic, TypeVar, Callable
from abc import abstractmethod
from mypy_extensions import trait
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@trait
class Visitor(Generic[T1]):
@abstractmethod
def visit_node(self, t: 'Node') -> T1:
...
@trait
class NodeVisitor(Visitor[T1]):
def __init__(self, function: Callable[['Node'], T1]) -> None:
self.f = function
def visit_node(self, t: 'Node') -> T1:
return self.f(t)
class Node(object):
__slots__ = ('name',)
def __init__(self, name: str) -> None:
self.name = name
def accept(self, visitor: 'NodeVisitor[T2]') -> 'T2':
raise RuntimeError('Not implemented')
class DivNode(Node):
def accept(self, visitor: 'NodeVisitor[T2]') -> 'T2':
return visitor.visit_node(self)
def is_node(node: Node) -> bool:
return bool(node.name)
print(DivNode('div').accept(NodeVisitor(is_node))) Output: » PYTHONFAULTHANDLER=1 mypyc bug.py && python -c 'import bug'
running build_ext
building 'bug' extension
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/local/opt/openssl/include -I/Users/sobolev/Documents/github/returns/.venv/lib/python3.7/site-packages/mypyc/lib-rt -I/Users/sobolev/Documents/github/returns/.venv/include -I/Users/sobolev/.pyenv/versions/3.7.7/include/python3.7m -c build/__native.c -o build/temp.macosx-10.14-x86_64-3.7/build/__native.o -O3 -Werror -Wno-unused-function -Wno-unused-label -Wno-unreachable-code -Wno-unused-variable -Wno-unused-command-line-argument -Wno-unknown-warning-option
clang -bundle -undefined dynamic_lookup -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/sobolev/.pyenv/versions/3.7.7/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/sobolev/.pyenv/versions/3.7.7/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/openssl/lib -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/local/opt/openssl/include build/temp.macosx-10.14-x86_64-3.7/build/__native.o -o /Users/sobolev/Documents/github/returns/bug.cpython-37m-darwin.so
[1] 13343 segmentation fault python -c 'import bug' When the second
|
OK there are a handful of issues with traits at play, all involving things that I didn't expect to work but did think that we produced error messages at runtime for?
We definitely definitely need to be able to produce error messages here. |
Only sort of, I guess. |
This prevents a bunch of segfaults Closes #9001. Closes #8360. It doesn't close either of them in a satisfactory way, though. Really they would like actual support, which I've opened as mypyc/mypyc#754. Related to mypyc/mypyc#655. (At some point there used to be working dynamic checks for at least one of these cases. Not sure when that broke.)
There are two issues here: that we don't support what this code does, and that it segfaults. I've opened mypyc/mypyc#754 for the former and submitted #9248 for the latter. I guess three issues: that you can't use TypeQuery usefully. There's any easy fix to that, though, which is that TypeQuery doesn't need to be a trait at all. I'll fix that too. |
This makes them usable by non-compiled plugins. This fixes the user-blocking issue in #9001.
This makes them usable by non-compiled plugins. This fixes the user-blocking issue in #9001.
This prevents a bunch of segfaults. Closes #9001. Closes #8360. It doesn't close either of them in a satisfactory way, though. Really they would like actual support, which I've opened as mypyc/mypyc#754. Related to mypyc/mypyc#655. (At some point there used to be working dynamic checks for at least one of these cases. Not sure when that broke.)
@msullivan I have tried your fix. There's no segfault right now. Thanks! Unfortunately, it still does not solve my problem. Because, I still cannot use Here's what happens right now: from mypy.types import TypeTranslator
class KindTranslator(TypeTranslator):
... Outputs:
Is it possible to make
I am going to try this out by adding Context on why I do need this: dry-python/returns#631 This is a very powerful tool, but plugin users a limited not to use it, because of the current API. So, I guess this should be available as a public API. |
One more thing: from mypy.type_visitor import TypeVisitor
class KindTranslator(TypeVisitor):
... This also fails with This looks like a bug, doesn't it? Update: no, this is not a bug. |
Now these types can be extended from plugin code. More context: #9001 (comment)
…10125) Currently a very important tool - which `NodeVisitor` definitely is - is not available to be used in a 3rd party code. Because currently inheriting from `NodeVisitor` raises an exception: `TypeError: interpreted classes cannot inherit from compiled traits` A developer has a single choice to replicate the same visitor pattern by themselves, which is not really convenient. So, I propose to make this consistent with `TypeVisitor`, which already allows having interpreted subclasses. Refs a9fa9ab Refs #9001 Refs #9602
Hi @sobolevn , I stumbled upon this issue through the documentation in your returns project. Given that it is still referenced, I am assuming that the issue still persists, making the conversion of an emulated higher kinded type to a concrete MyPy type impossible. However, it has led to several new commits regarding TypeQuery, and the idea of a TypeTranslator class. So I was wondering, what is the current status after 4 years? |
Uh oh!
There was an error while loading. Please reload this page.
There's a bug when working with
TypeQuery
. It is literally unusable.or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
Just take any type inside a plugin and pass it to
TypeQuery
:And provide any source code to trigger this plugin:
I would expect
TypeQuery
to work. Without the segfault.Do you see the same issue after installing mypy from Git master?
0.780
Full list: https://github.com/dry-python/returns/blob/master/setup.cfg#L122
The text was updated successfully, but these errors were encountered: