Abstract_paths_from_template and optional fields?

I noticed that using tk.abstract_paths_from_template with optional fields, paths that do not contain the optional fields are not returned. I guess this is because one glob pattern is used for all. Trying to understand the code for this.

paths = ["BOO_059_320_bg01", "BOO_059_420"]
template_def: "{Sequence}_{Shot}_{Scene}[_{Element}]"
tk.abstract_paths_from_template(tk.templates["template_def"], {"Sequence": "BOO"})

In a case like this, only the paths that have the optional field are detected.

Found that calling template.parent returns a template for the directory, but breaks optional keys by including them unconditionally.

Digging into this more. Template.parent for a path is os.path.dirname(template.definition)
However template.definition has no knowledge of optional fields - they are shown as normal fields. There is template._repr_def which contains the original definition, and it works with os.path.dirname! So I will fork some of the functions for the time being.

I will put together a PR on GitHub when I have a better understanding of the issue, comments especially from SG peeps welcome.
It happens right here, when we use template.parent: