Skip to content

add dedicated support for attrs #2404

Done
7 of 7 issues completed
Done

Description

@carljm
No description provided.

Sub-issues

Sub-issues

7 of 7 Issues completed
Loading 7 sub-issues

Activity

  1. added this to the Stable milestone on Jan 8, 2026
  2. added
    library
    on Jan 8, 2026
  3. hynek commented on Jan 12, 2026

    @hynek

    wow I came begging for one particular feature and found this late xmas present! 😻

    LMK if you need any help/advice!


    As a starter (and the reason I came here): could y'all implement attrs's underscore prefix stripping? 😇

    I.e. defining an attribute called _foo creates an __init__(self, foo) because there's no such thing as a private argument and you wouldn't call your argument that yourself either (this goes back to attrs's philosophy of helping you to write classes the way you would write them). This is currently by far my biggest annoyance with ty's LSP server.

    Should I open an subissue (hilariously I've never used that feature so not sure how)?

  4. carljm commented on Jan 13, 2026

    @carljm
    ContributorAuthor

    Filed #2474!

    Don't thank us too much until we actually do it! We currently have this slated for stable, but there's a lot of stuff slated for stable -- this might end up being a next-Christmas present 😆

  5. my1e5 commented on Feb 5, 2026

    @my1e5

    Thanks for opening this tracking issue! Just wanted to raise that

    should also be a sub-issue.

  6. ktbarrett commented on Mar 9, 2026

    @ktbarrett

    #3008 is related.

  7. modified the milestones: Stable, on May 22, 2026
  8. yashovardhan99 commented on Jun 22, 2026

    @yashovardhan99

    If possible, can we add support for attrs custom init as well.

    A small example:

    @frozen(init=False)
    class Document:
        """A document."""
    
        content: str
        metadata: dict[str, Any] = field(factory=dict)
    
        def __init__(
            self,
            content: str,
            metadata: dict[str, Any] | None = None,
            source: str | None = None,
        ) -> None:
            metadata_dict = copy.copy(metadata) if metadata is not None else {}
            if source is not None:
                metadata_dict["source"] = source
            self.__attrs_init__(content=content, metadata=metadata_dict) # Object of type `Self@__init__` has no attribute `__attrs_init__`
  9. 9 remaining items

  10. hynek commented on Nov 26, 2026

    @hynek

    I've ran ty 0.0.111 against the pyrefly and mypy typing tests and the things you mention are looking good – I think I'll be able to merge quite a bit into baseline!

    The only things I've noticed are:

    @x.converter is missing wholesale.

    foo = object
    if attrs.has(foo) or attrs.has(foo):
        foo.__attrs_attrs__

    Doesn't pass so it doesn't on pyrefly on 1.15 so dunno what is going on.

    Ty caught an error in the type checks where we use multiple inheritance with slotted classes 🤓

    I've decided to refactor the typing checks a bit: python-attrs/attrs#1670

    Do you think there's anything missing?

    Thanks again for tackling this!

  11. hynek commented on Nov 26, 2026

    @hynek

    ah so, I've added a test regarding private attributes and it didn't work and it seems like Ty's attrs integration doesn't work if it's running inside the repo as a first-party package.

    I have now worked around it by passing ty check --config 'environment.root=[]' but could there be a nicer way? Are there any precedents within Ty?

  12. added a commit that references this issue on Nov 26, 2026
  13. sharkdp commented on Nov 26, 2026

    @sharkdp
    Contributor

    Thanks again for tackling this!

    Thank you for the feedback!

    @x.converter is missing wholesale.

    ty has support for converters for other dataclass-flavors, so x: int = field(converer=...) should already work). We do not have support for @<field>.converter yet, but that shouldn't be too hard to add. It looks like this hasn't even been released as a feature of attrs, though, so you're asking us to support upcoming features of attrs? 😄

    foo = object
    if attrs.has(foo) or attrs.has(foo):
        foo.__attrs_attrs__

    (was that supposed to be attr instead of attrs in one of the checks?)

    It looks like this could be fixed in attrs itself if this protocol was defined for ty as well. Then ty would understand the TypeGuard here and narrow foo accordingly.

    And I guess ty would have to synthesize that __attrs_attrs__ attribute on attrs classes.

    Ty caught an error in the type checks where we use multiple inheritance with slotted classes 🤓

    Hooray? 😄

    Do you think there's anything missing?

    A larger feature that we haven't implemented is support for unannotated fields, e.g.:

    @attr.define
    class User:
        id = attr.ib(type=int | None, default=None)

    I have a draft PR up for adding support, but it doesn't fit naturally into ty's architecture, unfortunately. Do you think that this is important to support?

    ah so, I've added a test regarding private attributes and it didn't work and it seems like Ty's attrs integration doesn't work if it's running inside the repo as a first-party package.

    Oh, yes. This is sort-of intentional. If a user has a module named attr(s) in their first-part source code, we don't want to start attributing any special behavior to symbols inside those modules. It could be worked around by moving the typing tests to a separate package, or by using something like you did, or maybe:

    [tool.ty.environment]
    extra-paths = ["src"]

    because we consider extra-paths to potentially contain 3rd party code.

  14. sharkdp commented on Nov 26, 2026

    @sharkdp
    Contributor

    It looks like this could be fixed in attrs itself if this protocol was defined for ty as well. Then ty would understand the TypeGuard here and narrow foo accordingly.

    And I guess ty would have to synthesize that __attrs_attrs__ attribute on attrs classes.

    I guess it's not easily possible to distinguish ty from other type checkers right now. I was planning to propose a change to python/typing, e.g. a TYPE_CHECKER_NAME variable, that would allow library authors to identify type-checkers in a standardized way. But that's a long-term thing, so for now I will just add support for __attrs_attrs__ / AttrsInstance to ty itself: astral-sh/ruff#30958

  15. sharkdp commented on Nov 26, 2026

    @sharkdp
    Contributor

    @x.converter is missing wholesale.

    astral-sh/ruff#30959

  16. hynek commented on Nov 26, 2026

    @hynek

    @x.converter is missing wholesale.

    ty has support for converters for other dataclass-flavors, so x: int = field(converer=...) should already work). We do not have support for @<field>.converter yet, but that shouldn't be too hard to add. It looks like this hasn't even been released as a feature of attrs, though, so you're asking us to support upcoming features of attrs? 😄

    Ha, I kinda lost track! This was one of the features that fell a bit off the wagon because Pyright/Pylance sucked all the air out of the ecosystem and there was little reason to implement new features. :( I'll come back once I released a new version; guess I finally have a reason to wade thru all the slop PRs. 🤢

    Do you think there's anything missing?
    A larger feature that we haven't implemented is support for unannotated fields, e.g.:

    @attr.define
    class User:
    id = attr.ib(type=int | None, default=None)
    I have a draft PR up for adding support, but it doesn't fit naturally into ty's architecture, unfortunately. Do you think that this is important to support?

    I do not. That was mostly a compat feature back in the 2.x times. I don't think they're widely used in maintained software.

    ah so, I've added a test regarding private attributes and it didn't work and it seems like Ty's attrs integration doesn't work if it's running inside the repo as a first-party package.

    Oh, yes. This is sort-of intentional. If a user has a module named attr(s) in their first-part source code, we don't want to start attributing any special behavior to symbols inside those modules. It could be worked around by moving the typing tests to a separate package, or by using something like you did, or maybe:

    [tool.ty.environment]
    extra-paths = ["src"]
    because we consider extra-paths to potentially contain 3rd party code.

    yay much cleaner: python-attrs/attrs@a5c6c87

    [__attrs_attrs__]
    But that's a long-term thing, so for now I will just add support for attrs_attrs / AttrsInstance to ty itself: astral-sh/ruff#30958

    So I understand all I have to do is fixing the attr.has typo and wait for a ty release?

  17. sharkdp commented on Nov 26, 2026

    @sharkdp
    Contributor

    Ha, I kinda lost track!

    No worries. This is implemented in ty now, so whenever attrs releases this feature, it should already work in ty.

    I do not. That was mostly a compat feature back in the 2.x times. I don't think they're widely used in maintained software.

    Ok, good. In this case, I'm choosing not to implement support for this in ty.

    So I understand all I have to do is fixing the attr.has typo and wait for a ty release?

    Yes, here you go: https://github.com/astral-sh/ty/releases/tag/0.0.112

  18. sharkdp commented on Nov 26, 2026

    @sharkdp
    Contributor

    I think this completes attrs support for now. Everyone, please feel free to file new issues (or comment here) if a feature seems to be missing or if something isn't working correctly.

  19. added a commit that references this issue on Nov 27, 2026
  20. AdrianSosic commented on Nov 27, 2026

    @AdrianSosic

    Hi @sharkdp, I think I found another false positive. While the converters/validators work in isolation, it seems that their interaction is not yet handled correctly. Here a minimal example.

    from typing import Any
    
    from attrs import Attribute, define, field
    
    
    def to_float(value: float | None) -> float:
        return 0.0 if value is None else float(value)
    
    
    def validator(self: Any, attribute: Attribute, value: Any) -> None: ...
    
    
    @define
    class Item:
        value: float = field(
            default=0.0,
            converter=to_float,
            validator=validator,
        )
    
    
    def make_item(value: float | None) -> Item:
        return Item(value)  # ty: Expected `float`, found `float | None`

    Let me know if I should open a separate issue for it 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

attrsdataclasseslibrary

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions