Deprecating a method's kwarg

Consider a function

foo(x,y; c=true) = ...

I would like to remove the c=true kwarg in such a way that a depwarn is emitted upon its use.

What’s the proper way to do this?

One way is to use c=nothing as the default, then throw the depwarn if another value is used. e.g.

1 Like
function foo(x, y; c=nothing)
    if c !== nothing
    c = true

The problem with this is that the function is going to be reconstituted in a different (optional) package, so when that happens, we’ll have a “method overwrite” issue.

I’m not sure I understand the situation: could you give a bit more detail?

Sure. (Sorry for the initial vagueness; I overoptimized my MWE.)

Currently LightGraphs has a kwarg that specifies whether or not graph files are compressed on save: roughly,

savegraph(fn, g, format=LGFormat(); compress=true)

We are no longer supporting compressed saves in LightGraphs, instead moving that functionality out to GraphIO.jl via a new LGCompressedFormat() format. (Ignore reading the graphs for this exercise; we’ve got that covered.)

The problem is that we’re at 1.x in LightGraphs and don’t want to make a major breaking change here because it’s way too soon to be considering LightGraphs 2.x, so the idea here is to make a deprecation warning pointing to GraphIO.jl if the user really needs compressed graph saves.

The preferred behavior for LightGraphs is as follows:

  1. saving a graph without a compress kwarg: save uncompressed.
  2. saving a graph with compress=true kwarg: depwarn, then save uncompressed.
  3. saving a graph with compress=false kwarg: info that compress is no longer a supported option, then save uncompressed.

For GraphIO, we would like to basically move this function over while introducing a new LGCompressedFormat() type that governs its use. When both GraphIO and LightGraphs are loaded,

  1. savegraph(fn, g; compress=true) calls GraphIO.savegraph(fn, g, LGCompressedFormat())
  2. savegraph(fn, g) calls LightGraphs.savegraph(fn, g)
  3. savegraph(fn, g, compress=false) (ultimately) calls LightGraphs.savegraph(fn, g)

Sorry for all the detail, but you asked for it :slight_smile:

Something like the following should work.

In LightGraphs.jl:

function savegraph(fn, g; compress=false)
    if compress
        return _savegraph_compress(fn, g, true)
    ... # uncompressed saving code
function _savegraph_compress(fn, g, _)
    depwarn("To save compressed, first load the `GraphIO` package")
    savegraph(fn, g)

In GraphIO.jl:

function LightGraphs._savegraph_compress(fn, g, _::Bool)
    ... # compressed saving code

Ah, since you’re making the type more specific in GraphIO, it won’t be an overwrite. I think I get it now. Thanks.