Coder Social home page Coder Social logo

Comments (13)

Marenz avatar Marenz commented on September 28, 2024

I forgot to add: If I remove the python part in my example, it works again

from sybil.

Marenz avatar Marenz commented on September 28, 2024

I just realized we do a heavy amount of our own parsing in the linked module.

Specifically here, we use example.end to find the end of the sample and in previous versions that seemed to
match with position before the terminating tripple quotes.

from sybil.

cjw296 avatar cjw296 commented on September 28, 2024

I forgot to add: If I remove the python part in my example, it works again

Is the example even executed/checked if you do this?

Specifically here, we use example.end to find the end of the sample and in previous versions that seemed to match with position before the terminating tripple quotes.

It did, but that was a bug, even appears in the release notes ;-)

from sybil.

Marenz avatar Marenz commented on September 28, 2024

It did, but that was a bug, even appears in the release notes ;-)

I was wondering about that..one persons bug is another persons feature 😆

We're doing that extra parsing because we try to have the original line numbers, but maybe I am just not using some existing features to get the same?

Anyway, closing since it's not a bug, but appreciate any pointers if I can do it better.

from sybil.

cjw296 avatar cjw296 commented on September 28, 2024

Can you try changing this:

        example_code = textwrap.dedent(
            example.document.text[example.start : example.end]
        )

to this:

        example_code = example.parsed

from sybil.

cjw296 avatar cjw296 commented on September 28, 2024

Am I right in reading your code as only running pylint over the example rather than actually executing the examples?

Getting line numbers right is hard, especially if you're modifying the source before executing it. Sybil used to do that style of modification, but I managed to eradicate it by using compile flags instead, partly because getting it right is so hard.

Nonetheless, here's what Sybil does to get this right:

def __call__(self, example: Example) -> None:
# There must be a nicer way to get line numbers to be correct...
source = pad(example.parsed, example.line + example.parsed.line_offset)
code = compile(source, example.path, 'exec', flags=self.flags, dont_inherit=True)

from sybil.

Marenz avatar Marenz commented on September 28, 2024

Am I right in reading your code as only running pylint over the example rather than actually executing the examples?

I mean, executing them fully is certainly desirable, but many of our code requires remote end points for most examples to work :)

I'll try your suggestions as soon as I have some time :) Thanks!

from sybil.

Marenz avatar Marenz commented on September 28, 2024

def __call__(self, example: Example) -> None:
# There must be a nicer way to get line numbers to be correct...
source = pad(example.parsed, example.line + example.parsed.line_offset)
code = compile(source, example.path, 'exec', flags=self.flags, dont_inherit=True)

What I don't understand about this code (I do the padding the same), you use example.parsed, but that text bit has all the empty lines removed, thus it must also result in wrong line numbers the further you get to the end (more skipped blank lines).

Otherwise that whole hack I did would be obsolete as I could just use .parsed.

from sybil.

cjw296 avatar cjw296 commented on September 28, 2024

you use example.parsed, but that text bit has all the empty lines removed

I don't believe this is the case, can you show me an example of .parsed having empty lines removed?

Otherwise that whole hack I did would be obsolete as I could just use .parsed.

That's my hope :-) But, to get the line numbers right, you still have to prefix with the right number of newlines to match where the example was found in the source, and also the number of newlines between the start of the example and the actual line within the example, which is what the code on line 25 above is doing.

from sybil.

Marenz avatar Marenz commented on September 28, 2024

I don't believe this is the case, can you show me an example of .parsed having empty lines removed?

Sure, any example works for that for me.. I added the following code to the already linked snippets for the comparison:

        # Dedent the code example
        # There is also example.parsed that is already prepared, but it has
        # empty lines stripped and thus fucks up the line numbers.
        example_code = textwrap.dedent(
            example.document.text[example.start : example.end]
        )
        # Remove first line (the line with the triple backticks)
        example_code = example_code[example_code.find("\n") + 1 :]

        print(f"Example code: >\n{example_code}<")
        print(f"=================\n.parsed: >\n{example.parsed}<")

Output:

Example code: >
a = 2
# blank line follows this

b = 3
<
=================
.parsed: >
a = 2
# blank line follows this
b = 3
<

from sybil.

cjw296 avatar cjw296 commented on September 28, 2024

I added a test for this in https://github.com/simplistix/sybil/pull/116/files
It appears to show that no blank lines are removed by the MyST PythonCodeBlockParser.

It you could work up a similar PR to the one above that shows the problem you are experiencing, I can take a look.

Otherwise, can you confirm this verifies the behaviour is introduced somewhere in your code?

from sybil.

Marenz avatar Marenz commented on September 28, 2024

I'll see if I can make a test

from sybil.

Marenz avatar Marenz commented on September 28, 2024

#117

from sybil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.