SymPy makes REPL to stuck

When I tried the following in REPL, it gets stuck when I types the last line. I cannot keep typing. The command top shows that Julia is using all the CPU. Any suggestion on how to trouble shoot the problem?

I thought it might be due to the fact that I am using kitty terminal emulator. However, other terminal emulators have the same issue.

Also, it seems that running SymPy.jl code as a script does not have this issue.

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.11.2 (2024-12-01)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using SymPy

julia> @syms n
(n,)

julia> ex = 1/(n*(n+3))
    1    
─────────
nβ‹…(n + 3)

julia> sympy.apart(e
1 Like

Installed SymPy to check, happens to me too. The middle lines weren’t needed, anything typed after just using SymPy and sympy.apart( freezes the REPL. Windows Task Manager doesn’t say Julia is using all the CPU though. I have no earthly idea why writing a partial function call would freeze on a non-space character, and I’m very curious. Other properties freeze upon the parentheses too:

julia> sympy.apart
Callable SymPy method

julia> sympy.sin
Callable SymPy method

julia> sympy.cos( a

In any case, SymPy.apart or just apart (because it’s exported) works as documented:

julia> using SymPy

julia> @syms n
(n,)

julia> ex = 1/(n*(n+3))
    1
─────────
nβ‹…(n + 3)

julia> apart(ex)
      1        1
- ───────── + ───
  3β‹…(n + 3)   3β‹…n

To use properties of sympy, I could work around the freezing by assigning the property access to another variable, then doing the call separately:

julia> a = sympy.apart; a(ex) # note: different object from SymPy.apart
      1        1
- ───────── + ───
  3β‹…(n + 3)   3β‹…n

julia> let b = sympy.apart; b(ex) end # local variable works too, preferred
      1        1
- ───────── + ───
  3β‹…(n + 3)   3β‹…n

or avoiding function call syntax via the 1-argument piping operator:

julia> ex |> sympy.apart
      1        1
- ───────── + ───
  3β‹…(n + 3)   3β‹…n

or writing it in a string to be parsed and evaluated (@eval call or quoted expressions freeze):

julia> eval(Meta.parse("sympy.apart(ex)"))
      1        1
- ───────── + ───
  3β‹…(n + 3)   3β‹…n

Hopefully someone knows why the REPL would freak out at these particular calls in source or quoted expressions after SymPy is imported.

1 Like

My guess is that it’s the the REPL autocomplete code that is trying to obtain possible completions from the sympy object that is causing the issue. Can the autocomplete be turned off somehow?

1 Like

You could also try Julia 1.10 …

1 Like

Yes. Julia 1.10 does not have this issue.

Possibly related: REPL hints and tab completion freezes the REPL Β· Issue #55434 Β· JuliaLang/julia Β· GitHub

1 Like

Confirmed related, disabling the v1.11 REPL hinting in one of the ways that issue linked makes this go away for me, and re-enabling the hinting brings back the freeze. Before it did, using the up-arrow to find and rerun the line worked fine.

julia> Base.active_repl.options.hint_tab_completes = false
false

julia> sympy.apart(ex)
      1        1
- ───────── + ───
  3β‹…(n + 3)   3β‹…n

julia> Base.active_repl.options.hint_tab_completes = true
true

julia> sympy.apart(ex) # up-arrow to previous line
      1        1
- ───────── + ───
  3β‹…(n + 3)   3β‹…n

julia> sympy.apart(e

I’m surprised that hinting does type inference for an incomplete call, I thought it would only need to search the scopes for names. This also doesn’t explain why the a = sympy.apart; a(ex) workaround worked; does REPL hinting just not bother inferring a(e there? Whatever it does, I’d definitely want it to give up right after I type another character, on a single thread too.

2 Likes

I have added this to ~/.julia/confg/startup.jl

atreplinit() do repl
    # Turn off hint in Julia 1.11 to deal with SymPy problem
    Base.active_repl.options.hint_tab_completes = false
end
1 Like

Please check the pre-release of v1.11.3 (or julia nightly) as this should be fixed there (and save having to disable hints)

Specifically Very sluggish autocomplete with PyCall Β· Issue #54131 Β· JuliaLang/julia Β· GitHub was the issue that has been fixed.

3 Likes

The freezing issue has been resolved. However, we get another problem

julia> using SymPy
Precompiling SymPy...
  19 dependencies successfully precompiled in 11 seconds. 33 already precompiled.

julia> sympy.apart(33/4)
8.25000000000000

julia> @syms n
(n,)

julia> ex = 1/(n*(n+3))
    1    
─────────
nβ‹…(n + 3)

julia> sympy.apart(eβ”Œ Error: Error in the keymap
β”‚   exception =
β”‚    TypeError: in typeassert, expected REPL.REPLCompletions.MethodCompletion, got a value of type REPL.REPLCompletions.TextCompletion
β”‚    Stacktrace:
β”‚      [1] complete_keyword_argument
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPLCompletions.jl:1040
β”‚      [2] completions(string::String, pos::Int64, context_module::Module, shift::Bool, hint::Bool)
β”‚        @ REPL.REPLCompletions ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPLCompletions.jl:1379
β”‚      [3] complete_line(c::REPL.REPLCompletionProvider, s::REPL.LineEdit.PromptState, mod::Module; hint::Bool)
β”‚        @ REPL ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:637
β”‚      [4] #complete_line#14
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:428
β”‚      [5] complete_line
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:427
β”‚      [6] complete_line
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:369
β”‚      [7] edit_tab (repeats 2 times)
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:2419
β”‚      [8] #118
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:2465
β”‚      [9] #invokelatest#2
β”‚        @ ./essentials.jl:1055 [inlined]
β”‚     [10] invokelatest
β”‚        @ ./essentials.jl:1052 [inlined]
β”‚     [11] #30
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:1711
β”‚     [12] macro expansion
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:2861 [inlined]
β”‚     [13] macro expansion
β”‚        @ ./lock.jl:273 [inlined]
β”‚     [14] #282
β”‚        @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:2851
β”” @ REPL.LineEdit ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:2863

Odd that this issue wasn’t caught by the local testing in REPL: Limit method lookup when completing kwargs by IanButterworth Β· Pull Request #56963 Β· JuliaLang/julia Β· GitHub

Can you try REPL: Handle message from `complete_methods!` when max methods is hit by IanButterworth Β· Pull Request #57138 Β· JuliaLang/julia Β· GitHub
i.e. juliaup add pr57138 once that’s got green tests

OK. But I will need to figure out how to use run Julia modified with pull requests.

ufechner@framework:~$ juliaup add pr57138
Installing Julia pr57138-linux-x86_64
ufechner@framework:~$ julia +pr57138
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.12.0-DEV.1927 (2025-01-23)
 _/ |\__'_|_|_|\__'_|  |  IanButterworth:ib/repl_completion_fix/1a469315033 (fork: 1 commits, 0 days)
|__/                   |

So easy…

1 Like

Strange, I was spared on v1.11.3 for that example. Could it be platform-dependent, I’m on Windows.

julia> sympy.apart(33/4)
8.25000000000000

julia> @syms n
(n,)

julia> ex = 1/(n*(n+3))
    1
─────────
nβ‹…(n + 3)

julia> sympy.apart(ex)
      1        1
- ───────── + ───
  3β‹…(n + 3)   3β‹…n

EDIT: yes, hitting TAB gets me the error

It happens when I type sympy.apart(e and hit Tab.

Applying the patch pull request above does fix the problem!

Please comment on the PR with your reproducer, errors and versioninfo

2 Likes

Ah, I am not sure what β€œreproducer” mean.

Reproducer: How to create a Minimal, Reproducible Example - Help Center - Stack Overflow

I now see you have edited your post to say it has fixed the issue for you. So obviously you won’t have errors to add.

1 Like

Yes. When I first run it I forgot to use the version with the PR. I noticed this and tried it with the PR. The problem disappears.

1 Like