better static checks on initializing CheckedDict variables with dictionary literals

Open
#60 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
compilers

Research direction

Start with cinder/3.8/Lib/test/test_compiler/test_static/tests.py, especially test_compile_checked_dict_with_annotation_wrong_key_type and the similar wrong-value-type test. Run these tests and trace the static checker behavior for a CheckedDict annotation assigned a dictionary literal; done means key expressions are checked against T1 and values against T2, with the key mismatch reported directly.

Written by the indexing model from the issue text.

Description

sp-adoption staticpython

a4a71ae
2021-12-20

What program did you run?
from __static__ import CheckedDict

class B: pass

def testfunc():
x: CheckedDict[B, int] = {object():42}
return x

This program is found in cinder/3.8/Lib/test/test_compiler/test_static/tests.py. This test is labeled test_compile_checked_dict_with_annotation_wrong_key_type. There is a similar test labeled test_compile_checked_dict_with_annotation_wrong_value_type.

What happened?
compiler.errors.TypedSyntaxError: type mismatch: Exact[chkdict[object, Literal[42]]] cannot be assigned to Exact[chkdict[__main__.B, int]]
What should have happened?

We expected a compile-time error complaining that object cannot be assigned to B. In general, we expect that everytime the type checker sees x: CheckedDict[T1, T2] = { e1: e2, ... }, it checks every key expression e1 against T1, and every e2 against T2.

Dominant language
Python
Stars
3.8k
Forks
139
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from facebookincubator/MetaPython

All issues in facebookincubator/MetaPython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.