GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow parsing source code directly by bzoracler · Pull Request #54 · mypyc/ast_serialize

Conversation

@bzoracler

@bzoracler bzoracler commented Apr 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Resolves #21

Tests are part of python/mypy#21260

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bzoracler bzoracler mentioned this pull request Apr 17, 2026
Allow nativeparse to parse source code directly python/mypy#21260
Merged
@ilevkivskyi ilevkivskyi mentioned this pull request Apr 25, 2026
Support --package-root with native parser python/mypy#21321
Merged
@ilevkivskyi

ilevkivskyi commented Apr 25, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR! Me or Jukka will try to take a look next week. In the meantime, could you please resolve the merge conflict?

bzoracler reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bzoracler added 3 commits April 28, 2026 13:23
@bzoracler
Refactor serialisation to allow both file and source args
455153c
@bzoracler
Allow parse to receive both Python str and bytes source directly
bf34e0b
@bzoracler
Simplify Source into newtype and build UnicodeDecodeError for inv…
566ddc3
…alid bytes
@bzoracler
bzoracler force-pushed the parse-source branch from 1f293cc to 566ddc3 Compare April 28, 2026 02:21
bzoracler
bzoracler commented Apr 28, 2026
View reviewed changes
Comment thread src/serialize_ast.rs Outdated
Comment on lines +280 to +287
match pyo3::exceptions::PyUnicodeDecodeError::new_utf8(
obj.py(),
e.as_bytes(),
utf8_err,
) {
Ok(err) => Self::Error::from_value(err.into_any()),
Err(err) => err,
}

@bzoracler bzoracler Apr 28, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation taken from https://pyo3.rs/main/doc/src/pyo3/exceptions.rs#802-811 (PR https://github.com/PyO3/pyo3/pull/5668/changes), because new_err_from_utf8 is only in pyo3 0.29 which is not released yet.

The test is here: python/mypy@ac275e4

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
bzoracler
bzoracler commented Apr 28, 2026
View reviewed changes
Comment thread src/serialize_ast.rs Outdated
)> {
serialize_module(
source,
PySourceType::Python,

@bzoracler bzoracler Apr 28, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've hard-coded Python source type here, because mypy parsing functions never previously exposed an option to let the user treat source code directly as a .pyi stub source.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ilevkivskyi ilevkivskyi May 16, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to use the same logic as for serialize_module().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bzoracler reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bzoracler
Build options in detached closure
a07da63
@bzoracler

bzoracler commented Apr 28, 2026

Copy link
Copy Markdown
Contributor Author

Hmm...there might be some unnecessary allocations that are preventable. I'm going to do some benchmarking with another implementation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bzoracler
bzoracler marked this pull request as draft April 28, 2026 08:54
@bzoracler
Improve performance when working with bytes source
30bd655
@bzoracler
bzoracler marked this pull request as ready for review April 29, 2026 06:46
@bzoracler

bzoracler commented Apr 29, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

If we're willing to use &str in downstream functions instead of String (which IMO is fine), 30bd655 removes unnecessary allocations when working with Python builtins.bytes passed as source code. Microbenchmarks on a test machine (Intel Core i7-10750H CPU @ 2.60GHz, x86_64, 12 cores) show ~45% reduction in allocation during type conversion from PyBytes to &str (tested with 100 MB source code), and the type conversion itself runs about 6x faster (although execution speed is dominated by parsing, so this speedup doesn't matter as much).

Performance of builtins.str passed as source code does not seem to be improvable unless stable-abi is increased to ["pyo3/abi3-py310"], after which we can use this extraction function. I tried the same microbenchmarks after increasing the stable-abi, got similar allocation reduction and a huge speedup in type conversion (thousands of times faster, but again the execution time is dominated by parsing so doesn't really matter here).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hauntsaninja
hauntsaninja approved these changes May 10, 2026
View reviewed changes

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, but will wait for Ivan or Jukka.

Mypy has dropped support for Python 3.9, so I would also take a PR dropping 3.9 support and increasing the stable ABI

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bzoracler reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bzoracler
Fix typo in error message
6c7f7e5
ilevkivskyi
ilevkivskyi reviewed May 16, 2026
View reviewed changes

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, but I have couple comments about respecting the file name when parsing source. It is better be consistent in case mypy (or other user) will decide to open/read file manually for some reason and pass the source. They should get identical result as when passing the file name.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/serialize_ast.rs Outdated
PySourceType::Python,
skip_function_bodies,
options,
false,

@ilevkivskyi ilevkivskyi May 16, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be false, we should always respect the file name provided.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bzoracler reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread src/serialize_ast.rs Outdated
)> {
serialize_module(
source,
PySourceType::Python,

@ilevkivskyi ilevkivskyi May 16, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to use the same logic as for serialize_module().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bzoracler reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
bzoracler added 2 commits May 17, 2026 18:17
@bzoracler
Re-unify handling of source and file path serialisation
2ab6a96
@bzoracler
formatting
3272fbc
@bzoracler

bzoracler commented May 17, 2026

Copy link
Copy Markdown
Contributor Author

@ilevkivskyi Whoops, I thought providing source contents was only used via mypy --command TEXT, which was why I split the handling between path-based and source contents. I've re-unified this now (and diff is now much smaller).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bzoracler
bzoracler requested a review from ilevkivskyi May 17, 2026 06:27
ilevkivskyi
ilevkivskyi approved these changes May 17, 2026
View reviewed changes

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this looks good as a good starting point. I think we will need some more things to make native parser a full replacement, but it is probably better to move in small increments. I will merge this now, release a new version, then we can continue discussion in the mypy PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ilevkivskyi
ilevkivskyi merged commit 87b7843 into mypyc:main May 17, 2026
20 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bzoracler
bzoracler deleted the parse-source branch May 19, 2026 05:56
ilevkivskyi added a commit to python/mypy that referenced this pull request May 21, 2026
@bzoracler @pre-commit-ci @ilevkivskyi
Allow nativeparse to parse source code directly (#21260)
6f0e77b
This is the mypy counterpart of
mypyc/ast_serialize#54

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ivan Levkivskyi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ilevkivskyi ilevkivskyi ilevkivskyi approved these changes
@hauntsaninja hauntsaninja hauntsaninja approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Support parsing strings in addition to files

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@bzoracler @ilevkivskyi @hauntsaninja
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.