Skip to content

Comments

[ty] Add public class_name and class_module_name methods to NominalInstanceType#23147

Merged
sharkdp merged 1 commit intoastral-sh:mainfrom
knutwannheden:make-nominal-instance-class-public
Feb 9, 2026
Merged

[ty] Add public class_name and class_module_name methods to NominalInstanceType#23147
sharkdp merged 1 commit intoastral-sh:mainfrom
knutwannheden:make-nominal-instance-class-public

Conversation

@knutwannheden
Copy link
Contributor

Add two public convenience methods to NominalInstanceType so that external crates can retrieve the class name and defining module of an instance type without needing access to the pub(super) class() method or pub(crate) types like ClassType and ClassLiteral:

  • class_name(&self, db) -> &Name: Returns the class name (e.g. "str")
  • class_module_name(&self, db) -> Option<&ModuleName>: Returns the fully qualified module name where the class is defined (e.g. "pathlib" for pathlib.Path), or None for scripts/notebooks.

This enables tools that use ty_python_semantic as a library to extract structured type information from instance types for use cases like type attribution in code transformation tools.

…nalInstanceType`

Add two public convenience methods to `NominalInstanceType` so that external crates can retrieve the class name and defining module of an instance type without needing access to the `pub(super)` `class()` method or `pub(crate)` types like `ClassType` and `ClassLiteral`:

- `class_name(&self, db) -> &Name`: Returns the class name (e.g. `"str"`)
- `class_module_name(&self, db) -> Option<&ModuleName>`: Returns the fully qualified module name where the class is defined (e.g. `"pathlib"` for `pathlib.Path`), or `None` for scripts/notebooks.

This enables tools that use `ty_python_semantic` as a library to extract structured type information from instance types for use cases like type attribution in code transformation tools.
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Feb 8, 2026
Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you.

@sharkdp sharkdp added the internal An internal refactor or improvement label Feb 9, 2026
@sharkdp sharkdp self-assigned this Feb 9, 2026
@sharkdp sharkdp merged commit b1b2e38 into astral-sh:main Feb 9, 2026
51 checks passed
@AlexWaygood
Copy link
Member

Hmm, I think this is the first time we've added pub methods specifically for external users who want to use ty as a library.

Unless we add comments to these methods to indicate why they're pub, I worry they'll be removed the next time I (or someone else) does a sweep like #19949 to remove unused code.

I also think we need to be clear to external users that we're not making any backwards compatibility guarantees for ty when it's used as a library. I don't think we should tie our hands if we decide we want to remove the Type::NominalInstance variant entirely in the future, or merge it with another variant, or split it up into multiple variants, etc.

@knutwannheden
Copy link
Contributor Author

@AlexWaygood I understand and agree with your reasoning. Even without backwards compatibility it would however be nice if the API remained open enough so that it can be used as a library.

@AlexWaygood
Copy link
Member

yeah, I'm not totally opposed to this kind of PR! Just want to make sure we've thought through the pros and cons clearly here. And I do think we should at the least definitely add a comment saying to these APIs are for external consumers, or they're quite likely to be deleted in a cleanup PR in the future IMO.

Out of interest, would you be happy to say what you're using ty as a library for? No worries if not, just curious

@knutwannheden
Copy link
Contributor Author

I'm working on a code transformation framework called OpenRewrite, which had up until recently only supported Java and a few other JVM languages. The ASTs are full fidelity in the sense that they retain all whitespace and comments (so a CST, really) as well as being fully type-attributed to allow transformations to query the types of expressions and declarations.

I tried quite a few different libraries to get type attribution for Python, but most weren't viable for different reasons (very slow, no longer maintained, bad type inference, etc.).

Using ruff/ty I can now either try to work with the LSP or build a small Rust application that extracts all type attribution in bulk for a source file.

@AlexWaygood
Copy link
Member

that's very cool, thanks for sharing! Good luck with the project

AlexWaygood added a commit that referenced this pull request Feb 16, 2026
…ss_name` and `NominalInstanceType::class_module_name`

These were added in #23147 for external users who may be using `ty_python_semantic` as a library, but, since they're unused by us I think they're at high risk of being deleted in a cleanup PR without comments to explain why they're there.
AlexWaygood added a commit that referenced this pull request Feb 16, 2026
…ss_name` and `NominalInstanceType::class_module_name` (#23339)

These were added in #23147 for
external users who may be using `ty_python_semantic` as a library, but,
since they're unused by us I think they're at high risk of being deleted
in a cleanup PR without comments to explain why they're there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants