Is my compilers assignment irrational?
Sorry, in complete generality this is an undecidable problem. Pick your favorite undecidable problem $P$ and an instance $a$, and write e.g.:
def value(a):
     if P(a):
         1
      else:
         &quot;Hello, world!&quot;

print(41 + value(a))

Under the (conservative!) assumption that all execution paths are possible, you see that the above is badly formed (value returns an integer or a string). That restriction is doable, and not too hard either:   Do a postorder traversal of your AST, recording the possible types of each node. If any node could have conflicting types, flag a (possible) error.