Skip to main content

What Types Are Needed for Typing Dynamic Objects? A Python-Based Empirical Study

  • Conference paper
  • First Online:
Programming Languages and Systems (APLAS 2023)

Abstract

Dynamic object-oriented languages, such as Python, Ruby, and Javascript are widely used nowadays. A distinguishing feature of dynamic object-oriented languages is that objects, the fundamental runtime data representation, are highly dynamic, meaning that a single constructor may create objects with different types and objects can evolve freely after their construction. While such dynamism facilitates fast prototyping, it brings many challenges to program understanding. Many type systems have been developed to aid programming understanding, and they adopt various types and techniques to represent and track dynamic objects. However, although many types and techniques have been proposed, it is unclear which one suits real dynamic object usages best. Motivated by this situation, we perform an empirical study on 50 mature Python programs with a focus on object dynamism and object type models. We found that (1) object dynamism is highly prevalent in Python programs, (2) class-based types are not precise to handle dynamic behaviors, as they introduce type errors for 52% of the evaluated polymorphic attributes, (3) typestate-based types, although mostly used in static languages, matches the behaviors of dynamic objects faithfully, and (4) some well-designed but still lightweight techniques for object-based types, such as argument type separation and recency abstraction can precisely characterize dynamic object behaviors. Those techniques are suitable for building precise but still concise object-based types.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 49.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 64.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    We refer the constructed types of panel1/panel2 by \(\tau _1\)/\(\tau _2\) in the rest of this paper.

  2. 2.

    In our example, only panel1 is not recent. However, in general, there can be many.

  3. 3.

    https://github.com/python/cpython/tree/3.9.

  4. 4.

    In such cases, there is no need to precisely distinguish the branches.

  5. 5.

    As a dynamic analysis, we can naturally simulate the precision analysis of them.

  6. 6.

    Even some static languages such as Java do not exclude them.

References

  1. Agesen, O.: The Cartesian product algorithm. In: Tokoro, M., Pareschi, R. (eds.) ECOOP 1995. LNCS, vol. 952, pp. 2–26. Springer, Heidelberg (1995). https://doi.org/10.1007/3-540-49538-X_2

    Chapter  Google Scholar 

  2. Ahmed, A., Fluet, M., Morrisett, G.: L\(^{3}\): a linear language with locations. Fundamenta Informaticae 77(4), 397–449 (2007)

    Google Scholar 

  3. Aiken, A., Foster, J.S., Kodumal, J., Terauchi, T.: Checking and inferring local non-aliasing. In: Proceedings of the ACM SIGPLAN 2003 Conference on Programming Language Design and Implementation, pp. 129–140 (2003)

    Google Scholar 

  4. Åkerblom, B., Stendahl, J., Tumlin, M., Wrigstad, T.: Tracing dynamic features in python programs. In: Proceedings of the 11th Working Conference on Mining Software Repositories, pp. 292–295 (2014)

    Google Scholar 

  5. Åkerblom, B., Wrigstad, T.: Measuring polymorphism in python programs. In: Proceedings of the 11th Symposium on Dynamic Languages, pp. 114–128 (2015)

    Google Scholar 

  6. Aldrich, J., Sunshine, J., Saini, D., Sparks, Z.: Typestate-oriented programming. In: Proceedings of the 24th ACM SIGPLAN Conference Companion on Object Oriented Programming Systems Languages and Applications, pp. 1015–1022 (2009)

    Google Scholar 

  7. Anderson, C., Giannini, P., Drossopoulou, S.: Towards type inference for JavaScript. In: Black, A.P. (ed.) ECOOP 2005. LNCS, vol. 3586, pp. 428–452. Springer, Heidelberg (2005). https://doi.org/10.1007/11531142_19

    Chapter  Google Scholar 

  8. Balakrishnan, G., Reps, T.: Recency-abstraction for heap-allocated storage. In: Yi, K. (ed.) SAS 2006. LNCS, vol. 4134, pp. 221–239. Springer, Heidelberg (2006). https://doi.org/10.1007/11823230_15

    Chapter  Google Scholar 

  9. Barbanera, F., Dezaniciancaglini, M., Deliguoro, U.: Intersection and union types: syntax and semantics. Inf. Comput. 119(2), 202–230 (1995)

    Article  MathSciNet  Google Scholar 

  10. Bierman, G., Abadi, M., Torgersen, M.: Understanding TypeScript. In: Jones, R. (ed.) ECOOP 2014. LNCS, vol. 8586, pp. 257–281. Springer, Heidelberg (2014). https://doi.org/10.1007/978-3-662-44202-9_11

    Chapter  Google Scholar 

  11. Blaudeau, C., Liu, F.: A conceptual framework for safe object initialization: a principled and mechanized soundness proof of the celsius model. Proc. ACM Program. Lang. 6(OOPSLA2), 729–757 (2022)

    Article  Google Scholar 

  12. Bravetti, M., et al.: Behavioural types for memory and method safety in a core object-oriented language. In: Oliveira, B.C.S. (ed.) APLAS 2020. LNCS, vol. 12470, pp. 105–124. Springer, Cham (2020). https://doi.org/10.1007/978-3-030-64437-6_6

    Chapter  Google Scholar 

  13. Castagna, G., Laurent, M., Nguyen, K., Lutze, M.: On type-cases, union elimination, and occurrence typing. Proc. ACM Program. Lang. 6(POPL), 1–31 (2022)

    Google Scholar 

  14. Chen, Z., Li, Y., Chen, B., Ma, W., Chen, L., Xu, B.: An empirical study on dynamic typing related practices in python systems. In: Proceedings of the 28th International Conference on Program Comprehension, pp. 83–93 (2020)

    Google Scholar 

  15. Choi, W., Chandra, S., Necula, G., Sen, K.: SJS: a type system for JavaScript with fixed object layout. In: Blazy, S., Jensen, T. (eds.) SAS 2015. LNCS, vol. 9291, pp. 181–198. Springer, Heidelberg (2015). https://doi.org/10.1007/978-3-662-48288-9_11

    Chapter  Google Scholar 

  16. Chugh, R., Herman, D., Jhala, R.: Dependent types for JavaScript. In: Proceedings of the ACM International Conference on Object Oriented Programming Systems Languages and Applications, pp. 587–606 (2012)

    Google Scholar 

  17. Eifrig, J., Smith, S., Trifonov, V.: Sound polymorphic type inference for objects. In: Proceedings of the Tenth Annual Conference on Object-Oriented Programming Systems, Languages, and Applications, pp. 169–184 (1995)

    Google Scholar 

  18. Fähndrich, M., Leino, K.R.M.: Declaring and checking non-null types in an object-oriented language. In: Proceedings of the 18th Annual ACM SIGPLAN Conference on Object-Oriented Programing, Systems, Languages, and Applications, pp. 302–312 (2003)

    Google Scholar 

  19. Furr, M., An, J.h., Foster, J.S., Hicks, M.: Static type inference for ruby. In: Proceedings of the 2009 ACM Symposium on Applied Computing, pp. 1859–1866 (2009)

    Google Scholar 

  20. Golovanov, I., Jakobsen, M.S., Kettunen, M.K.: Typestate inference for mungo: Algorithm and implementation. Online Material (2020)

    Google Scholar 

  21. Google: Pytype, a static type analyzer for python code. Online Material (2023)

    Google Scholar 

  22. Hassan, M., Urban, C., Eilers, M., Müller, P.: MaxSMT-based type inference for Python 3. In: Chockler, H., Weissenbacher, G. (eds.) CAV 2018, Part II. LNCS, vol. 10982, pp. 12–19. Springer, Cham (2018). https://doi.org/10.1007/978-3-319-96142-2_2

    Chapter  Google Scholar 

  23. Heidegger, P., Thiemann, P.: Recency types for analyzing scripting languages. In: D’Hondt, T. (ed.) ECOOP 2010. LNCS, vol. 6183, pp. 200–224. Springer, Heidelberg (2010). https://doi.org/10.1007/978-3-642-14107-2_10

    Chapter  Google Scholar 

  24. Holkner, A., Harland, J.: Evaluating the dynamic behaviour of python applications. In: Proceedings of the Thirty-Second Australasian Conference on Computer Science, vol. 91, pp. 19–28 (2009)

    Google Scholar 

  25. Igarashi, A., Pierce, B.C., Wadler, P.: Featherweight Java: a minimal core calculus for Java and GJ. ACM Trans. Program. Lang. Syst. (TOPLAS) 23(3), 396–450 (2001)

    Article  Google Scholar 

  26. Jensen, S.H., Møller, A., Thiemann, P.: Type analysis for JavaScript. In: Palsberg, J., Su, Z. (eds.) SAS 2009. LNCS, vol. 5673, pp. 238–255. Springer, Heidelberg (2009). https://doi.org/10.1007/978-3-642-03237-0_17

    Chapter  Google Scholar 

  27. Kaleba, S., Larose, O., Jones, R., Marr, S.: Who you gonna call: analyzing the run-time call-site behavior of ruby applications. In: Proceedings of the 18th ACM SIGPLAN International Symposium on Dynamic Languages, pp. 15–28 (2022)

    Google Scholar 

  28. Kent, A.M., Kempe, D., Tobin-Hochstadt, S.: Occurrence typing modulo theories. ACM SIGPLAN Not. 51(6), 296–309 (2016)

    Article  Google Scholar 

  29. Kouzapas, D., Dardha, O., Perera, R., Gay, S.J.: Typechecking protocols with Mungo and StMungo. In: Proceedings of the 18th International Symposium on Principles and Practice of Declarative Programming, pp. 146–159 (2016)

    Google Scholar 

  30. Lehtosalo, J.: Optional static typing for python. Online Material (2023)

    Google Scholar 

  31. Lerner, B.S., Politz, J.G., Guha, A., Krishnamurthi, S.: TeJaS: retrofitting type systems for JavaScript. ACM SIGPLAN Not. 49(2), 1–16 (2013)

    Article  Google Scholar 

  32. Madhavan, R., Komondoor, R.: Null dereference verification via over-approximated weakest pre-conditions analysis. ACM Sigplan Not. 46(10), 1033–1052 (2011)

    Article  Google Scholar 

  33. Maia, E., Moreira, N., Reis, R.: A static type inference for python. Proc. DYLA 5(1), 1 (2012)

    Google Scholar 

  34. McGugan, W.: Rich, a python library for rich text and beautiful formatting in the terminal. Online Material (2023)

    Google Scholar 

  35. Microsoft: Pyright, a static type checker for python. Online Material (2023)

    Google Scholar 

  36. Monat, R., Ouadjaout, A., Miné, A.: Static type analysis by abstract interpretation of python programs. In: 34th European Conference on Object-Oriented Programming (ECOOP 2020). Schloss Dagstuhl-Leibniz-Zentrum für Informatik (2020)

    Google Scholar 

  37. Oxhøj, N., Palsberg, J., Schwartzbach, M.I.: Making type inference practical. In: Madsen, O.L. (ed.) ECOOP 1992. LNCS, vol. 615, pp. 329–349. Springer, Heidelberg (1992). https://doi.org/10.1007/BFb0053045

    Chapter  Google Scholar 

  38. Pierce, B.C.: Types and Programming Languages. MIT Press, Cambridge (2002)

    MATH  Google Scholar 

  39. Pilkiewicz, A., Pottier, F.: The essence of monotonic state. In: Proceedings of the 7th ACM SIGPLAN Workshop on Types in Language Design and Implementation, pp. 73–86 (2011)

    Google Scholar 

  40. Rastogi, A., Swamy, N., Fournet, C., Bierman, G., Vekris, P.: Safe & efficient gradual typing for typescript. In: Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, pp. 167–180 (2015)

    Google Scholar 

  41. Richards, G., Hammer, C., Burg, B., Vitek, J.: The eval that men do. In: Mezini, M. (ed.) ECOOP 2011. LNCS, vol. 6813, pp. 52–78. Springer, Heidelberg (2011). https://doi.org/10.1007/978-3-642-22655-7_4

    Chapter  Google Scholar 

  42. Richards, G., Lebresne, S., Burg, B., Vitek, J.: An analysis of the dynamic behavior of JavaScript programs. In: Proceedings of the 31st ACM SIGPLAN Conference on Programming Language Design and Implementation, pp. 1–12 (2010)

    Google Scholar 

  43. Rondon, P.M., Kawaguchi, M., Jhala, R.: Low-level liquid types. ACM Sigplan Not. 45(1), 131–144 (2010)

    Article  MATH  Google Scholar 

  44. Salib, M.: Starkiller: a static type inferencer and compiler for Python. Ph.D. thesis, Massachusetts Institute of Technology (2004)

    Google Scholar 

  45. Smith, F., Walker, D., Morrisett, G.: Alias types. In: Smolka, G. (ed.) ESOP 2000. LNCS, vol. 1782, pp. 366–381. Springer, Heidelberg (2000). https://doi.org/10.1007/3-540-46425-5_24

    Chapter  Google Scholar 

  46. Strom, R.E., Yemini, S.: Typestate: a programming language concept for enhancing software reliability. IEEE Trans. Softw. Eng. SE-12(1), 157–171 (1986). https://doi.org/10.1109/TSE.1986.6312929

  47. Summers, A.J., Müller, P.: Freedom before commitment: a lightweight type system for object initialisation. In: Proceedings of the 2011 ACM International Conference on Object Oriented Programming Systems Languages and Applications, pp. 1013–1032 (2011)

    Google Scholar 

  48. Tobin-Hochstadt, S., Felleisen, M.: Logical types for untyped languages. In: Proceedings of the 15th ACM SIGPLAN International Conference on Functional Programming, pp. 117–128 (2010)

    Google Scholar 

  49. Van Rossum, G., Drake, F.L., Jr.: The Python Language Reference. Python Software Foundation, Wilmington (2014)

    Google Scholar 

  50. Vekris, P., Cosman, B., Jhala, R.: Refinement types for typescript. In: Proceedings of the 37th ACM SIGPLAN Conference on Programming Language Design and Implementation, pp. 310–325 (2016)

    Google Scholar 

  51. Vitousek, M.M., Kent, A.M., Siek, J.G., Baker, J.: Design and evaluation of gradual typing for python. In: Proceedings of the 10th ACM Symposium on Dynamic languages, pp. 45–56 (2014)

    Google Scholar 

  52. Wang, T., Smith, S.F.: Precise constraint-based type inference for Java. In: Knudsen, J.L. (ed.) ECOOP 2001. LNCS, vol. 2072, pp. 99–117. Springer, Heidelberg (2001). https://doi.org/10.1007/3-540-45337-7_6

    Chapter  Google Scholar 

  53. Wang, Y.: PySonar2: an advanced semantic indexer for python. Online Material (2019)

    Google Scholar 

  54. Wei, S., Xhakaj, F., Ryder, B.G.: Empirical study of the dynamic behavior of JavaScript objects. Softw. Pract. Exper. 46(7), 867–889 (2016)

    Google Scholar 

  55. Zhao, T.: Polymorphic type inference for scripting languages with object extensions. In: Proceedings of the 7th Symposium on Dynamic Languages, pp. 37–50 (2011)

    Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Dan Hao .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2023 The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd.

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Sun, K., Chen, S., Wang, M., Hao, D. (2023). What Types Are Needed for Typing Dynamic Objects? A Python-Based Empirical Study. In: Hur, CK. (eds) Programming Languages and Systems. APLAS 2023. Lecture Notes in Computer Science, vol 14405. Springer, Singapore. https://doi.org/10.1007/978-981-99-8311-7_2

Download citation

  • DOI: https://doi.org/10.1007/978-981-99-8311-7_2

  • Published:

  • Publisher Name: Springer, Singapore

  • Print ISBN: 978-981-99-8310-0

  • Online ISBN: 978-981-99-8311-7

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics