Skip to content
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

Glitchy nested end for sciml style #604

Closed
theabhirath opened this issue May 30, 2022 · 15 comments
Closed

Glitchy nested end for sciml style #604

theabhirath opened this issue May 30, 2022 · 15 comments
Labels
bug Something isn't working

Comments

@theabhirath
Copy link

In using the formatter for Metalhead.jl (FluxML/Metalhead.jl#163) with the sciml style, it seems to somehow want to keep the final two ends on the same line for testsets with a for. An example is given below:

@testset "VGG" begin @testset "VGG($sz, batchnorm=$bn)" for sz in [11, 13, 16, 19],
                                                            bn in [true, false]

    m = VGG(sz, batchnorm = bn)

    @test size(m(x_224)) == (1000, 1)
    if (VGG, sz, bn) in PRETRAINED_MODELS
        @test (VGG(sz, batchnorm = bn, pretrain = true); true)
    else
        @test_throws ArgumentError VGG(sz, batchnorm = bn, pretrain = true)
    end
    @test gradtest(m, x_224)
    GC.safepoint()
    GC.gc()
end end
@domluna
Copy link
Owner

domluna commented May 30, 2022

it looks like the @testset for is being lifted up for some reason. Looking at the PR it wasn't like that in the original source

@ChrisRackauckas
Copy link
Contributor

@YingboMa

@YingboMa
Copy link
Contributor

YingboMa commented Sep 1, 2022

It's because of https://github.com/SciML/SciMLStyle#tests-and-continuous-integration . It's applied generally.

@theabhirath
Copy link
Author

Is there a way to toggle that particular option in the configuration file?

@ToucheSir
Copy link

Bump on this. Is https://github.com/domluna/JuliaFormatter.jl/blob/master/src/styles/sciml/pretty.jl#L93-L120 responsible for unconditionally applying this formatting? If so, having an option to disable it and use the default p_begin behaviour would be swell.

@domluna
Copy link
Owner

domluna commented Jan 1, 2023

I think it might be. I have to take another look at this, macros are tricky, maybe #669 can be dealt with at the same time

@domluna
Copy link
Owner

domluna commented Jan 7, 2023

so it's indeed because of https://github.com/domluna/JuliaFormatter.jl/blob/master/src/styles/sciml/pretty.jl#L93-L120

Is this intentional then? How should we proceed @YingboMa @ToucheSir

toggling this isn't exactly what options are designed for.

Can we adjust this so that if it's a macrocall then it is intended and placed on a new line?

@domluna
Copy link
Owner

domluna commented Jan 7, 2023

so changing line 103 to

if length(stmts_idxs) == 1 && !(cst[2].head === :macrocall && !is_closer(cst[2][end]))

domluna added a commit that referenced this issue Jan 7, 2023
Do not join the single statement in a begin block if it's a macroblock,
i.e. a macrocall with whitespace separation such as "@foo a b".
@domluna
Copy link
Owner

domluna commented Jan 7, 2023

ok #673 this seems sufficient no?

@ToucheSir
Copy link

If I understand the rule correctly, this is something that depends on line length as well? I don't understand the test in #673 well enough to know whether that's the globally desirable behaviour, but if the example in the OP is fixed then that looks good to me.

@domluna
Copy link
Owner

domluna commented Jan 7, 2023

        stmts_idxs = 2:length(cst)-1
        # Don't nest into multiple lines when there's only one statement
        # and it's not a macroblock.
        if length(stmts_idxs) == 1 && !(cst[2].head === :macrocall && !is_closer(cst[2][end]))
            add_node!(t, Whitespace(1), s)
            add_node!(t, pretty(style, cst[2], s), s, join_lines = true)
            add_node!(t, Whitespace(1), s)
            add_node!(t, pretty(style, cst[end], s), s, join_lines = true)
        else

this block forces line length to explicitly not be considered. The wording in the doc Yingbo linked and the comment he wrote initially in the above code implies it's more so about having a singular expression/statement vs. length

@domluna
Copy link
Owner

domluna commented Jan 7, 2023

@YingboMa does this look reasonable?

@domluna
Copy link
Owner

domluna commented Jan 7, 2023

the original example becomes

julia> format_text(s, SciMLStyle()) |> print
@testset "VGG" begin
    @testset "VGG($sz, batchnorm=$bn)" for sz in [11, 13, 16, 19],
                                           bn in [true, false]

        m = VGG(sz, batchnorm = bn)

        @test size(m(x_224)) == (1000, 1)
        if (VGG, sz, bn) in PRETRAINED_MODELS
            @test (VGG(sz, batchnorm = bn, pretrain = true); true)
        else
            @test_throws ArgumentError VGG(sz, batchnorm = bn, pretrain = true)
        end
        @test gradtest(m, x_224)
        GC.safepoint()
        GC.gc()
    end
end

@domluna
Copy link
Owner

domluna commented Jan 16, 2023

is this fine to merge SciML people?

@ChrisRackauckas
Copy link
Contributor

That looks fine.

domluna added a commit that referenced this issue Jan 16, 2023
Do not join the single statement in a begin block if it's a macroblock,
i.e. a macrocall with whitespace separation such as "@foo a b".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants