Skip to content

Fix return type annotation of PyRanges.__new__ to enable method chaining in static type checkers - #170

Open
adamgayoso wants to merge 1 commit into
pyranges:masterfrom
adamgayoso:master
Open

Fix return type annotation of PyRanges.__new__ to enable method chaining in static type checkers#170
adamgayoso wants to merge 1 commit into
pyranges:masterfrom
adamgayoso:master

Conversation

@adamgayoso

@adamgayoso adamgayoso commented Aug 2, 2026

Copy link
Copy Markdown

Currently, PyRanges.__new__ is annotated as returning pr.PyRanges | pd.DataFrame. When a user instantiates a PyRanges object:

import pyranges1 as pr
import pandas as pd

df = pd.DataFrame({"Chromosome": ["chr1"], "Start": [0], "End": [100]})
gr = pr.PyRanges(df)

# Type checker error!
# "Item 'DataFrame' of 'PyRanges | DataFrame' has no attribute 'join_overlaps'"
result = gr.join_overlaps(other_gr)

Because PyRanges inherits from DataFrame, any function expecting a DataFrame already accepts a PyRanges instance without needing | pd.DataFrame in the annotation.

…ing in static type checkers (PyRanges instead of PyRanges | pd.DataFrame)
@endrebak

endrebak commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

I will have a look

@endrebak

endrebak commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

I do not remember why I wrote it like this. I wonder if this was due to the return type sometimes actually being pd.DataFrame (when Chromosome, Start, or End is missing). This is actually bad design. I should throw an Error instead.

I think I thought that it would break LSP if I did not support this but I was confused. Written before I had LLMs to discuss with. Will check that tests pass and do a minor version bump also.

@endrebak

endrebak commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Ok, so I am getting back into the groove and it seems like we intentionally want it to be PyRanges | DataFrame so that e.g.

gr.drop("Chromosome")

returns a DataFrame.

@endrebak

endrebak commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

See #171

@adamgayoso

Copy link
Copy Markdown
Author

Thanks, #171 looks good to me.

@endrebak

Copy link
Copy Markdown
Collaborator

Type checks fail. Will need to find the time to work on this. Perhaps Sunday?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants