-
Notifications
You must be signed in to change notification settings - Fork 32
Description
Hi,
First of all, thanks for this super useful lib!
So I caught a bug while using cog
in a python module related to the end-spec string (by default ]]]
) being present in some part of the file outside the cog spec block.
In my case I had a python type annotation with four levels of nesting, something like var: list[list[tuple[int, int]]]
, somewhere else in the file that had the cog spec, and because of that type annotation cog
was not able to run, with the following error message:
my_module.py(2): Unexpected ']]]'
The workaround for me in that case was to split that nested type annotation into multiple types aliases, but that was far from ideal.
I did look at the cog
code and the fix seems very straightforward (see "possible fix" section below). If confirmed, I would be happy to submit a PR with it.
How to reproduce
- Create a file with a cog spec + some occurrence of the end-spec string (by default
]]]
)
def my_func(param: list[list[tuple[int, int]]]) -> None: # <-- end-spec string here
...
# [[[cog
# import cog
# fnames = ['DoSomething', 'DoAnotherThing', 'DoLastThing']
# for fn in fnames:
# cog.outl("void %s();" % fn)
# ]]]
# [[[end]]]
- Run
cog
, it will fail
$ cog my_module.py
my_module.py(2): Unexpected ']]]'
cog version: 3.5.1
Python version: 3.10.13
Possible fix
My guess is that the check that currently is triggering the error should not exist. It is this one:
Lines 449 to 454 in 0ff1d7c
if self.is_end_spec_line(line): | |
raise CogError( | |
f"Unexpected {self.options.end_spec!r}", | |
file=file_name_in, | |
line=file_in.linenumber(), | |
) |
I think it should not be there because at that point the current line
is not inside a cog-spec block, so actually any occurrence of an end-spec string in that line (as in my case) should be allowed.