Is there a way to make a "shadow target" which wou...
# plugins
b
Is there a way to make a "shadow target" which would allow me to re-use existing rule logic? Specifically I want to be able to infer Python import dependencies from a file I'm generating using another target. Currently the Python Infer rule expects a
PythonSourceField
. Can I synthesize a target or more specifically, a
PythonSourceField
?
f
You can subclass
PythonSourceField
and include that subclass on your custom target. Python-related code will still see it as a
PythonSourceField
.
👍 1
b
Right, but my thing isn't a Python source, so the parser will choke and die.
f
You could also try writing a codegen rule that targets
PythonSourceField
. That might work, but I don’t know definitively that it will.
b
Yeah I was kinda thinking that too, but does inference work for generated targets?!
And even still, the full inference rule for Python looks for other fields like
interpreter_constraints
🤔
It seems I want a full synthetic
python_source
Full disclosure, I'm looking at adding a full Jupyter Notebook plugin (first internally, then upstream) and need to parse my imports. The notebook file is just JSON, but concatenating the code blocks gives me Python. There'd need to be discussions, since technically notebooks are multi-language, but for the PoC it's all Python baby
It'd be easy to do in-Pants because I can modify the inference rules to take the contents instead of the source. But I can't do that in my plugin outside-of-Pants
h
Would it make sense to modify Pants's code itself to be more plugin-friendly? I think we're open to changes like that 🙂
b
Yeah. That might be a separate discussion though 🤔
So for now, I know it's only Python notebooks, so I'm going to imitate the built-in Python dep inference code. When I upstream, we can bend the dep inference rules to operate on content and then we can invoke the dep inference on the generated content
@witty-crayon-22786 for visibility. I think this maybe smells of a problem we might face in other places
f
it might be tangential, but https://github.com/pantsbuild/pants-jupyter-plugin may be of interest to you
b
Yeah, I think that is somewhat of a "poor man's" solution to the problem. I think it can definitely live harmoniously for "out-of-repo" notebooks, but nothing beats first-class in-repo notebook support
w
And even still, the full inference rule for Python looks for other fields like
interpreter_constraints
🤔
it needs to here as well… your notebook target probably still needs that field
b
Yeah I added that and the resolve field
w
it seems like multiple levels of codegen would get one step closer. but it’s true that dependency inference won’t run on generated code currently, which means we require plugins to pre-specify the deps of the code that they will generate. mostly intentional, because we don’t want e.g. protobuf to have to run to calculate dependencies.