Skip to content

fix(types): improve type hints for mlx_lm.utils.load method#919

Open
rahuliyer95 wants to merge 3 commits intoml-explore:mainfrom
rahuliyer95:fix/better-type-hints
Open

fix(types): improve type hints for mlx_lm.utils.load method#919
rahuliyer95 wants to merge 3 commits intoml-explore:mainfrom
rahuliyer95:fix/better-type-hints

Conversation

@rahuliyer95
Copy link

@rahuliyer95 rahuliyer95 commented Feb 22, 2026

Problem

The mlx_lm.utils.load returns a Tuple[nn.Module, TokenizerWrapper] when return_config=False and Tuple[nn.Module, TokenizerWrapper, Dict[str, Any]] when return_config=True. Since the return type is controlled by a function argument, the types can improved to determine the correct return type based on the value of return_config.

Solution

We can use the @overload decroator to help provide the proper type hints to the callers based on the value of return_config.

@awni
Copy link
Member

awni commented Feb 23, 2026

Thanks but it seems unnecessary. Also would prefer not to reorder the positions of the arguments just in case...

@awni awni closed this Feb 23, 2026
@rahuliyer95
Copy link
Author

Thanks but it seems unnecessary. Also would prefer not to reorder the positions of the arguments just in case...

I understand the concern about reordering the positions of the arguments, but using mlx-lm in a project with mypy complains about wrong return type which can be easily avoided with this change.

@awni
Copy link
Member

awni commented Feb 23, 2026

Hmm.. ok fair enough. Will you update the PR to use the old ordering and then we can merge it?

@awni awni reopened this Feb 23, 2026
@angeloskath
Copy link
Member

Just to be clear, mypy complains because the return type is a Union and you don't somehow verify that it is one of the two cases in the union when you pass it to a different function right?

It feels like the issue is not in mlx-lm and even though the @overload would help mypy deduce the type better I think the return type is fine and don't really like having the function definition replicated 3 times for little benefit...

Wdyt @rahuliyer95 ?

@rahuliyer95
Copy link
Author

Just to be clear, mypy complains because the return type is a Union and you don't somehow verify that it is one of the two cases in the union when you pass it to a different function right?

Yes that's correct.

It feels like the issue is not in mlx-lm and even though the @overload would help mypy deduce the type better I think the return type is fine and don't really like having the function definition replicated 3 times for little benefit...

Wdyt @rahuliyer95 ?

I believe the benefit is high because without this every consumer of mlx-lm would have to have a check in their code which can be avoided with this overload. Here is some prior art I found for other libraries like Tranformers, PyTorch which do the same.

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.

3 participants